#Setting gcTime on prefetched data

5 messages · Page 1 of 1 (latest)

ivory kindle
#

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?

lavish basin
#

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

ivory kindle
#

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

lavish basin