T
TanStack3y ago
xenial-black

Understanding SSR & persisting query cache

We currently have an SPA using React Query with a local query cache. Some of the queries to populate the cache can be a bit slow, so having a local storage query cache persistence is a livesaver - it means the app loads straight away with slightly stale data, while that data is being refreshed in the background. I am considering implementing SSR to get some of the benefits of that but I'm a bit confused about how it fits together. Is it possible to use a local storage persisted query client with SSR or are these two things fundamentally incompatible concepts? For instance, is it possible to use SSR to populate the cache once and then use the persisted query cache from there on? Thanks!
1 Reply
equal-aqua
equal-aqua3y ago
Absolutely possible, yes. Keep in mind that if you hard reload the page then, you'd SSR that page again including queries, and after that we'd rehydrate from localstorage. The query with the latest dataUpdatedAt timestamp would "win". Likely this would be the SSR query for the current page, but if there are other queries in localstorage, that would be put into the cache as well.

Did you find this page helpful?