T
TanStack7mo ago
fair-rose

Refetch on changing persister buster

I am using a local storage persister with a maxage and a buster. When the data is initially fetched, it goes into the persister and reloading the page retrieves it from storage fine. The issue is when the persister buster is changed (e.g. version change), it resets the local storage data to empty, but useQuery just happily reads out this empty data, instead of detecting that its been cleared and refetching from fresh. I'm assuming the same thing will happen when the persister reaches maxAge as well. How can i make the query refetch when the persister storage is expired?
5 Replies
ratty-blush
ratty-blush7mo ago
it resets the local storage data to empty
that's not what's happening. what does "empty" even mean? We call persister.removeClient(), which will delete the data in the store: https://github.com/TanStack/query/blob/2e1282ca038f270e3b03d048fdae429bb9d3e3af/packages/query-persist-client-core/src/persist.ts#L86-L89 since hydrate is never called, the queries are not in the cache, so it's like as if you had a completely new beginning without restored data, so your queries should be in pending state and happily fetch again on the first usage
GitHub
query/packages/query-persist-client-core/src/persist.ts at 2e1282ca...
🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query. - TanStack/query
fair-rose
fair-roseOP7mo ago
i played around some more before, and setting initialData on the usequery was causing the storage to load that in when the buster was changed (i did see the local storage entry disappear and be regenerated), but not refetch removing initialData, when buster is changed, then it does properly refetch i thought initialData was just meant to be what it returns while its fetching. is this just a quirk of the interaction between usequery and the persister?
ratty-blush
ratty-blush7mo ago
i thought initialData was just meant to be what it returns while its fetching
that's placeholderData not a quirks; initialData is put into the cache
fair-rose
fair-roseOP7mo ago
ah right, is there any genuine use case of initialData in this situation? initial data -> into storage -> usequery sees storage so doesnt fetch seems a bit pointless
ratty-blush
ratty-blush7mo ago
staleTime defines if there should be a background refetch once initialData is received or not so it depends how old that data is if you always want a refetch, prefer placeholderData

Did you find this page helpful?