persistWithLocalStorage is hydrating with the wrong cacheTime #1600
Send to my agentDescribe the bug
It appears that after hydrating from local storage, the cacheTime gets reset back to the default of 5 minutes.
I use 1000 * 60 * 60 * 24 which is 86400000 ms (24 hours). You can see in my GIF that this is correctly set at first, but as soon as I refresh the page and it hydrates from local storage, the cacheTime is set back to the default of 300000 ms (5 minutes).
It will then garbage collect the queries after 5 minutes if you navigate away from the app, which removes them from local storage, rendering local storage pretty useless...
To Reproduce https://codesandbox.io/s/react-query-persist-bug-4k32q
- Click on some buttons to save some queries
- Observe in devtools that the
cacheTimeis correct (86400000) - Refresh the page
- Observe that the
cacheTimeis now 300000 - Navigate away from the codesandbox for 5 minutes and come back to it
- Observe that there are no longer any queries saved in local storage
Expected behavior
Uses the correct cacheTime so queries are not forced out of local storage early.
Screenshots

Desktop (please complete the following information):
- OS: macOS
- Browser: Chrome
- Version: 3.5.6
Additional context Add any other context about the problem here.
But as far as I understood, you don't have to set blanket options like this, and can set them per query, is that right?
Yes, that's right, it should work.
looking at what is actually stored in the localstorage, it seems that properties like
staleTimeandcacheTimeare not part of the internal state, and that's why they are just not stored:
it seems to just work with default options because they will be set even for dehydrated queries if they have not specific cacheTime set.
Ah, that makes sense! Funny thing is, it looks like there was some code from this PR that already got merged and would fix it (adding cacheTime into
dehydrateQueryandhydratefunctions):However, what's strange is it no longer appears on master (https://github.com/tannerlinsley/react-query/blob/master/src/hydration/hydration.ts) and I don't have it in my node_modules after installing3.5.11.Was it never released with V3? I did some basic testing by manually adding the code into my node_moduleshydration.jsfile (just without serializePositiveNumber and deserializePositiveNumber) and it definitely fixes the problem.[edit] Somehow completely missed that this commit removes it https://github.com/tannerlinsley/react-query/commit/eb301fc908cc456352699041e9717bf232e354a3#diff-e96de446ebe6331f178837e79073d10448ca24c309f0fc9478c3ccaa89129817 🤦♀️ Ignore this comment
There were some problems with hydrating the cache time because sometimes servers and clients have different GC requirements. You can define a client cache time when constructing a QueryClient like @melanieseltzer suggested.
But maybe there is also some misconception about the cache time. The cache time is more of a memory management option because the GC timer starts counting from the moment a query is not being used anymore. You can specify a cache time of 24 hours, but as long as a query is being rendered on screen, this will not have any effect. Which means that the specified cache time does not really mean anything from a freshness perspective. If the query was hydrated after a year, the GC would start counting to 24 hours from that moment on, and only if the data was not rendered on screen. The stale time can be used to specify if data is considered fresh or not. I guess
gcTimewould be a better name.I guess gcTime would be a better name.
@boschni I made the exact same proposal a while back: https://github.com/tannerlinsley/react-query/discussions/1217
This is all very insightful!
So it seems to me that it should be listed in the docs for persist here https://react-query.tanstack.com/plugins/persist-localstorage that you need to define a cacheTime for QueryClient manually to override the default. Otherwise, persist straight up won't work right (as described in my original issue description).
I can contribute a PR to add to the docs, if ya'll agree with the above :)
Yeah, let's get a PR going and we can keep discussing there 👍
tannerlinsley added labelsdocumentation
tannerlinsley added labelsenhancement
tannerlinsley closed this
:tada: This issue has been resolved in version 3.5.14 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
tannerlinsley added labelsreleased
Sign in to join this conversation.
After poking around a little, it seems to function correctly if
cacheTimeis set as a default when creating the client, rather than as an option foruseQuery🤔 Like this:But as far as I understood, you don't have to set blanket options like this, and can set them per query, is that right?