Initial Data as Cache
I want to use react query as a offline backup cache. However I want to save a whole database table. Would it make sense to fetch all data like with pagination)
api/ratings
and then manually add all the routes like
api/ratings/1
api/ratings/2
api/ratings/3
So I always have all data in the cache just in case I loose connection.
Is this a valid way of doing it? Or is this something react query is not made to be.
https://tanstack.com/query/latest/docs/framework/react/guides/initial-query-data#initial-data-from-the-cache-with-initialdataupdatedatInitial Query Data | TanStack Query React Docs
There are many ways to supply initial data for a query to the cache before you need it: Declaratively: Provide initialData to a query to prepopulate its cache if empty Imperatively: Using initialData...
2 Replies
conscious-sapphire•9mo ago
Hi, I'm doing something similar but I only store what the user has already fetched while navigating through the app.
I'm still experiencing with the offline feature and it look like the persisting mode for TS query is nice for that. However I'm not sure if it is 100% bullet proof.
If you want to store the whole database (or only some table) on the client I would not recommend using TS query because it would be too heavy to have to loop through all the possible fetch queries.
I'm not familiar with any library for that as I never used any, But you should look at something like replicache.
extended-salmon•9mo ago
You can persist the query cache and give stuff a long
gcTime but its not a true "local first" library with a sync engine and the like