Skip to content

Commit 6d9abc6

Browse files
committed
fix: track active provider for npm fallback pagination and guard cache against stale username
1 parent 80b2d51 commit 6d9abc6

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

app/composables/npm/useUserPackages.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export function useUserPackages(username: MaybeRefOrGetter<string>) {
3030
// --- Incremental loading state (npm path) ---
3131
const currentPage = shallowRef(1)
3232

33+
/** Tracks which provider actually served the current data (may differ from
34+
* searchProvider when Algolia returns empty and we fall through to npm) */
35+
const activeProvider = shallowRef<'npm' | 'algolia'>(searchProvider.value)
36+
3337
const cache = shallowRef<{
3438
username: string
3539
objects: NpmSearchResult[]
@@ -62,6 +66,7 @@ export function useUserPackages(username: MaybeRefOrGetter<string>) {
6266
// registry which uses `maintainer:` search (matches all maintainers,
6367
// not just the primary owner that Algolia's owner.name indexes).
6468
if (response.objects.length > 0) {
69+
activeProvider.value = 'algolia'
6570
cache.value = {
6671
username: user,
6772
objects: response.objects,
@@ -74,7 +79,8 @@ export function useUserPackages(username: MaybeRefOrGetter<string>) {
7479
}
7580
}
7681

77-
// --- npm registry: initial page ---
82+
// --- npm registry: initial page (or Algolia fallback) ---
83+
activeProvider.value = 'npm'
7884
cache.value = null
7985
currentPage.value = 1
8086

@@ -110,8 +116,8 @@ export function useUserPackages(username: MaybeRefOrGetter<string>) {
110116
*/
111117
async function fetchMore(manageLoadingState = true): Promise<void> {
112118
const user = toValue(username)
113-
const provider = searchProvider.value
114-
if (!user || provider !== 'npm') return
119+
// Use activeProvider: if Algolia fell through to npm, we still need pagination
120+
if (!user || activeProvider.value !== 'npm') return
115121

116122
if (cache.value && cache.value.username !== user) {
117123
cache.value = null
@@ -142,7 +148,7 @@ export function useUserPackages(username: MaybeRefOrGetter<string>) {
142148
)
143149

144150
// Guard against stale response
145-
if (user !== toValue(username) || provider !== searchProvider.value) return
151+
if (user !== toValue(username) || activeProvider.value !== 'npm') return
146152

147153
if (cache.value && cache.value.username === user) {
148154
const existingNames = new Set(cache.value.objects.map(obj => obj.package.name))
@@ -185,16 +191,18 @@ export function useUserPackages(username: MaybeRefOrGetter<string>) {
185191
}
186192
}
187193

188-
// asyncdata will automatically rerun due to key, but we need to reset caache/page
194+
// asyncdata will automatically rerun due to key, but we need to reset cache/page
189195
// when provider changes
190-
watch(searchProvider, () => {
196+
watch(searchProvider, newProvider => {
191197
cache.value = null
192198
currentPage.value = 1
199+
activeProvider.value = newProvider
193200
})
194201

195-
// Computed data that uses cache
202+
// Computed data that uses cache (only if it belongs to the current username)
196203
const data = computed<NpmSearchResponse | null>(() => {
197-
if (cache.value) {
204+
const user = toValue(username)
205+
if (cache.value && cache.value.username === user) {
198206
return {
199207
isStale: false,
200208
objects: cache.value.objects,
@@ -207,8 +215,8 @@ export function useUserPackages(username: MaybeRefOrGetter<string>) {
207215

208216
/** Whether there are more results available to load (npm path only) */
209217
const hasMore = computed(() => {
210-
// Non-npm providers fetch everything in one request
211-
if (searchProvider.value !== 'npm') return false
218+
// Algolia fetches everything in one request; only npm needs pagination
219+
if (activeProvider.value !== 'npm') return false
212220
if (!cache.value) return true
213221
// npm path: more available if we haven't hit the server total or our cap
214222
const fetched = cache.value.objects.length

0 commit comments

Comments
 (0)