Skip to content

Commit a0a7a2f

Browse files
committed
fix(colada): avoid tracking loaders effect
Fix #744
1 parent 043c8bc commit a0a7a2f

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

src/data-loaders/defineColadaLoader.spec.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,5 +445,67 @@ describe(
445445
expect(entry1.deps.size).toBe(1)
446446
expect(entry1.active).toBe(true)
447447
})
448+
449+
it('marks loader queries as inactive when navigating between pages with different parameters', async () => {
450+
const useLoaderWithParam = defineColadaLoader({
451+
query: async (to) => `data-${to.params.id}`,
452+
key: (to) => ['page-loader', to.params.id as string],
453+
})
454+
455+
const router = getRouter()
456+
router.addRoute({
457+
path: '/',
458+
component: defineComponent({
459+
template: `<div><p id="home">Home</p></div>`,
460+
}),
461+
})
462+
router.addRoute({
463+
name: 'loader',
464+
path: '/loader/:id',
465+
meta: { loaders: [useLoaderWithParam] },
466+
component: defineComponent({
467+
setup() {
468+
const { data, error, isLoading } = useLoaderWithParam()
469+
return { data, error, isLoading }
470+
},
471+
template: `<div><p id="data">{{ data }}</p></div>`,
472+
}),
473+
})
474+
475+
const pinia = createPinia()
476+
477+
const wrapper = mount(RouterViewMock, {
478+
global: {
479+
plugins: [[DataLoaderPlugin, { router }], router, pinia, PiniaColada],
480+
},
481+
})
482+
483+
const queryCache = useQueryCache(pinia)
484+
485+
// navigate back and forth between two loader pages
486+
await router.push('/loader/1')
487+
expect(wrapper.find('#data').text()).toBe('data-1')
488+
489+
await router.push('/')
490+
expect(wrapper.find('#home').text()).toBe('Home')
491+
492+
await router.push('/loader/2')
493+
expect(wrapper.find('#data').text()).toBe('data-2')
494+
495+
await router.push('/')
496+
expect(wrapper.find('#home').text()).toBe('Home')
497+
498+
const entry1 = queryCache.get(['page-loader', '1'])!
499+
const entry2 = queryCache.get(['page-loader', '2'])!
500+
501+
expect(entry1).toBeDefined()
502+
expect(entry2).toBeDefined()
503+
504+
// Both entries should be inactive with no deps
505+
expect(entry1.deps.size).toBe(0)
506+
expect(entry1.active).toBe(false)
507+
expect(entry2.deps.size).toBe(0)
508+
expect(entry2.active).toBe(false)
509+
})
448510
}
449511
)

src/data-loaders/defineColadaLoader.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,7 @@ export function defineColadaLoader<Data>(
218218
// remove the data loader effect scope so that queries
219219
// can be marked as inactive
220220
useQueryCache()
221-
.getEntries({
222-
exact: true,
223-
key: toValueWithParameters(options.key, to),
224-
})[0]
221+
.get(toValueWithParameters(options.key, to))
225222
?.deps.delete(router[DATA_LOADERS_EFFECT_SCOPE_KEY])
226223

227224
// avoid double reload since calling `useQuery()` will trigger a refresh
@@ -460,6 +457,12 @@ export function defineColadaLoader<Data>(
460457
// this lets pinia colada track queries
461458
const ext = useDefinedQuery()
462459

460+
// remove the data loader effect scope so that queries
461+
// can be marked as inactive when navigating away
462+
useQueryCache()
463+
.get(toValueWithParameters(options.key, route))
464+
?.deps.delete(router[DATA_LOADERS_EFFECT_SCOPE_KEY])
465+
463466
// TODO: add watchers only once alongside the entry
464467
// update the data when pinia colada updates it e.g. after visibility change
465468
watch(ext!.data, (newData) => {

0 commit comments

Comments
 (0)