Repository navigation
[vue-query] Function options created in setup() keep unmounted components alive until gcTime #12713
Replies: 4 comments
|
This is expected. An inactive query keeps the last The inline Module scope is the right fix, with option functions reading per-instance values from // service-keys-query.ts
export function useServiceKeys(productUid: MaybeRefOrGetter<string>) {
return useQuery({
queryKey: ['service-keys', productUid],
queryFn: fetchServiceKeysForKey,
retry: (failureCount: number) => failureCount < 1,
})
}If you don't need the row cache after leaving the page, use a shorter The Vue Reactivity guide and the |
|
One caution about the suggested For an explicit eviction at a route boundary, I checked |
|
You can use |
|
Your analysis is correct, and the retention chain you traced is exactly how v5 stores it — this is a real (and under-discussed) consequence of the cache design, not a misdiagnosis. Why it happens (design context) An inactive query (unmounted, waiting out Mitigations, in the order I'd apply them
On the upstream side, your report is the kind that would land well as a discussion in TanStack/query itself: a plausible design improvement is for the cache to store a sanitized options snapshot on inactive queries (data-behavior fields like Bottom line: your fix (module-scope everything) is the correct app-level mitigation, |
Uh oh!
There was an error while loading. Please reload this page.
Summary
When a component unmounts, its inactive query stays in the cache until
gcTimeruns out, as documented. Whatsurprised us is that the query also keeps the last options it was given, including every function:
queryFn,retry,select,initialData,refetchInterval. If those functions were created insidesetup(), they keep that component's closure scope alive, and that scope can reach the whole page.In our app, leaving a page with many rows kept a large number of DOM nodes and much of the heap alive for
5 minutes. When we moved every option function to module scope, the page was freed as soon as we left it.
Minimal example
The inline
retrycaptures nothing, but it still keeps the component instance alive after unmount. In the above example it shares the same context as theonSelectwhich includes a reference toemit. In Vueemitbinds to the instance, and the instance includes a reference to theparent, which leads to the whole page.Retention chain:
What we checked
All with
@tanstack/vue-query5.108.4 and Vue 3.5.46, in Chrome and in Node with--expose-gc(WeakRef plusa forced GC):
queryFnthat readsprops, and the inlineretryabove.queryClient.clear(), so the cache is the retainer;queryKeythroughQueryFunctionContext, even with theemit-capturingonSelectstill in place. The queries stay cached;only the component is freed.
onSelectfrees the page but still keepspropsalive. How much leaks depends on what otherclosures in the same
setup()capture, so you can't tell by reading the option function itself.Questions
gcTime? We assume so, because an inactive query can still be refetched (e.g.refetchQueries({ type: 'all' })), which needsoptions.queryFn.useQueryreference showoption functions created in
setup(), e.g.queryFn: () => fetchPost(props.id). For a small componentthat's harmless, but on a large page with many rows it keeps the page alive for
gcTime. Would it be worth updating the docs to include some info about this particular issue?Happy to share the reproduction (a small Vite + vue-query project with a vitest spec that proves the retention
with
WeakRef) if it helps.All reactions