T
TanStack2y ago
extended-salmon

Query set default cacheTime

Hello, I'm trying to set the staleTime and cacheTime to infinity in the queryClient default options but the typescript is giving me a warning that I can't set the cacheTime within defualtoptions -> queries (staleTime can be set there fine). Am I missing something? thanks!
export const queryClient = new QueryClient({
defaultOptions: {
queries: {
// staleTime defines how long the data is considered fresh.
// During this time, no background refetch will occur, and the cached data will be used.
retry: 2,
retryDelay: 5000,
staleTime: Infinity, // Data will never become stale
},
mutations: {
// mutation settings can also be defined here.
// mutations generally do not have a staleTime as they are one-time operations,
// but you can add retry or other settings if needed.
retryDelay: 5000,
retry: 2, // Retries failed mutations up to 3 times
},
},
});
export const queryClient = new QueryClient({
defaultOptions: {
queries: {
// staleTime defines how long the data is considered fresh.
// During this time, no background refetch will occur, and the cached data will be used.
retry: 2,
retryDelay: 5000,
staleTime: Infinity, // Data will never become stale
},
mutations: {
// mutation settings can also be defined here.
// mutations generally do not have a staleTime as they are one-time operations,
// but you can add retry or other settings if needed.
retryDelay: 5000,
retry: 2, // Retries failed mutations up to 3 times
},
},
});
6 Replies
funny-blue
funny-blue2y ago
@Miketoast cacheTime is now gcTime
extended-salmon
extended-salmonOP2y ago
thank you! I'm curious where this is in the docs, maybe I'm just bad at reading them
funny-blue
funny-blue2y ago
Are you looking at the docs for an older version maybe? Happens all the time to me, when coming from a Google search.
extended-salmon
extended-salmonOP2y ago
here's something strange, gcTime: Infinity works fine in the QueryClient default options but UseQueryOptions accepts
staleTime: Infinity,
cacheTime: Infinity,
staleTime: Infinity,
cacheTime: Infinity,
seems like there's still a mix of the two
xenial-black
xenial-black2y ago
gcTime is v5, cacheTime is everything below that. Maybe you have multiple versions in your node_modules, maybe you import one from @tanstack/react-query and one from react-query (which is v3). Hard to say but there definitely is no cacheTime anywhere in v5
funny-blue
funny-blue2y ago
The web site is clearly showing that you are reading old documentation, but sometimes one misses it. When coming from Google, very often I am directed to v4 (and sometimes v3). Maybe the site can check 'if coming from Google, show version last'?

Did you find this page helpful?