Setting gcTime on prefetched data
Scenario:
Using
prefetchQuery
on the serverside to fetch some data for the page. The page that is loaded does not actively have a useSuspenseQuery
call for that particular piece of data, it is only used on other pages. The useSuspenseQuery
sets a gcTime of Infinity to disable garbage collection on the prefetched data. The prefetch call also sets gcTime
of Infinity.
The problem:
The prefetched data stored in the query cache gets a gcTime of "5 minutes" when there are no active queries subscribed to it, since there is nobody there to tell it otherwise. That means there doesn't seem to be a way to keep the prefetched data around until it's actually used by a query hook, without just calling a "dummy" hook on every page to make sure gcTime is set?3 Replies
stormy-gold•9mo ago
If you're using HydrationBoundary, you can set some defaultOptions to be used on hydration. Set gcTime there, or set a global default on the client for a specific range of keys with queryClient.setQueryDefaults
realistic-cyanOP•9mo ago
thanks that sounds like it will work.
Intuitively it seems to me that the gcTime passed to prefetch calls should carry over through dehydration
or maybe the concept of garbage collection is a bit weird when applied to prefetched data, since presumably if you’re prefetching it there should be a reason and you’d want it to stick around
stormy-gold•9mo ago
Yeah I get that. We generally don't dehydrate "options" of a query but I understand how gcTime could be different
If you use defaults instead of inline options it also works