Is it possible to persist cache query? #92
-
Beta Was this translation helpful? Give feedback.
Replies: 17 comments 13 replies
-
I assume you mean you want to persist the query cache? Looking at the code, the cache is just an array called |
Beta Was this translation helpful? Give feedback.
-
You're more than welcome to do try that out, but as of today, it's not a first class citizen mostly because invalidation would need to be set up to essentially clear out old persisted queries and that's something that would be difficult to make great for a majority of use cases. |
Beta Was this translation helpful? Give feedback.
-
Would you accept a PR that decouples the cache implementation? The default implementation would function the same as it does now. A |
Beta Was this translation helpful? Give feedback.
-
I would be open to discussing a proposal
|
Beta Was this translation helpful? Give feedback.
-
@deevus Did you make progress on persisting and seeding the cache? I'm interested in using react-query in a React Native app and with the app lifecycle, seeding the cache between app restarts is an important consideration. |
Beta Was this translation helpful? Give feedback.
-
This is really naive, but you could try this: import { queryCache } from 'react-query'
// Persist to wherever using the super-secret `queries` object
const queriesWithData = Object.values(queryCache.queries).map(query => ({
queryKey: query.queryKey,
data: query.state.data
}))
localStorage.setItem('queries', JSON.stringify(queriesWithData))
// Hydrate from localStorage
const queriesWithData = JSON.parse(localStorage.getItem('queries'))
queriesWithData.forEach(query => {
queryCache.setQueryData(query.queryHash, query.data)
}) This is highly experimental and undocumented, so you can be my guinea pig :) |
Beta Was this translation helpful? Give feedback.
-
I got busy but would still love this. @tannerlinsley that looks very interesting :) |
Beta Was this translation helpful? Give feedback.
-
@tannerlinsley And use the |
Beta Was this translation helpful? Give feedback.
-
Yeah!
|
Beta Was this translation helpful? Give feedback.
-
I'm also very interested by an offline-first approach! I'll try to be a guinea pig and experiment with this! 😄 |
Beta Was this translation helpful? Give feedback.
-
https://github.com/LuudJanssen/localforage-cache worked a treat, especially setting its key like this:
|
Beta Was this translation helpful? Give feedback.
-
i think we should make this official to hydrate the cache-post-SSR. @tannerlinsley--is there a different means to hydrate post-SSR? |
Beta Was this translation helpful? Give feedback.
-
Thanks! Here I share the hook I created. The implementation worked out for me in React Native and is applicable for React, just need to change AsyncStorage for whatever you use as localStorage: Calling Read hook in UI component: const {data} = useReadItem(item); Read hook export const useReadItem = (item?: string) => {
const queryKey = ['QUERY-STRING', String(item)];
return useQuery(queryKey, restRequest, {
enabled: address,
refetchOnWindowFocus: true,
onSuccess: (data) => writeToStorage(queryKey, data),
});
}; Manage storage Hook import {queryCache} from 'react-query';
import AsyncStorage from '@react-native-community/async-storage';
// Persist to wherever using the super-secret object
const writeToStorage = async (queryKey: string, data: any) => {
let storageData = await AsyncStorage.getItem('queries');
storageData = {
...JSON.parse(storageData ?? '{}'),
[queryKey]: data,
};
AsyncStorage.setItem('queries', JSON.stringify(storageData));
};
// Hydrate from localStorage
const readFromStorage = async () => {
const storageData = await AsyncStorage.getItem('queries');
if (storageData !== null) {
const queriesWithData = JSON.parse(storageData);
for (const queryKey in queriesWithData) {
const data = queriesWithData[queryKey];
const queryKeyParsed = JSON.parse(queryKey);
queryCache.setQueryData(queryKeyParsed, data);
}
}
};
export {readFromStorage, writeToStorage}; What I liked about this approach is that you can choose which queries to store and you only access local storage once when the app is accesed for the first time using a useEffect and calling |
Beta Was this translation helpful? Give feedback.
-
Yes we'd need something like this in a React Native project to persist over pause/resume presumably. |
Beta Was this translation helpful? Give feedback.
-
React query v3 has an experimental support for persisting data to localstorage using "persist-localstorage" 😄 |
Beta Was this translation helpful? Give feedback.
-
@tannerlinsley how efficient is it to persist the cache ? |
Beta Was this translation helpful? Give feedback.
-
Does createWebStoragePersistor-experimental uses "cacheTime" QueryClient property? Because I persist the cache but its forever in localStorage. |
Beta Was this translation helpful? Give feedback.
This is really naive, but you could try this:
This is highly experimental and undocumented, so you can be my guinea pig :)