How to stop a certain key from being stored in async storage?
Hello, forgive me if this has been documented elsewhere.
I was wondering if there is a way to make it so certain keys don't get persisted into the async storage for caching? I tried providing
cacheTime and staleTime as 0 but I can still see it in the cache.
My use case is pretty weird. I have react-query in a chrome extension (if you are unfamiliar, think react-native application). I have the network requests cached to the async storage so that the values I want are available offline.
On top of this ^, I like react-queries api, hooks, suspense, etc, so I'm also using it to interface with the async storage itself! So for example if the user changes their settings, I call a mutation which just does await AsyncStorage.set(). Likewise, I read from the async storage with a query. I dont want these being cached because it means that anytime I read, it will then proceed to be written again by the async persistor from the paragraph above. I do want to retain the fact that if there are two instances of useQuery with the same key, they share the same data.
^ Any ideas how to stop the additional write? I have the following ideas:
1. There is some prop that I don't know that I need to set to stop it from going into the cache.
2. Update the createAsyncStoragePersister's setItem to just ignore certain keys
3. I'm crazy for trying to use useQuery to access async storage especially when the react-query is being cached into async storage ๐19 Replies
secure-lavenderโข3y ago
Hi. We did a PoC with local storage persister some times ago. We used meta option to flag the queries we would like to store. Code is here: https://codesandbox.io/s/dkpu8
secure-lavenderโข3y ago
Note this was done in RQ v3 so APi might have changed since
fascinating-indigoOPโข3y ago
Wow that worked! Is this the right way?
"right"
secure-lavenderโข3y ago
๐คทโโ๏ธ๐
@TkDodo ๐ฎ ?
rival-blackโข3y ago
using
shouldDehydrateQuery is definitely the right way. If you want to find the queries to store via the meta field or something else is up to you. Please note that with v4, you'd want to use the <PersistQueryClientProvider> instead of calling persistQueryClient manually.
also, note that shouldDehydrateQuery per default is implemented to only persist successful queries. If you use your own logic, you likely want to incorporate that check as well, because persisting loading / error queries it not only pointless, but also not easily possible because Errors are not json serializable.fascinating-indigoOPโข3y ago
Yep got the first part! Not sure I follow the second part, I just have this:
Are you saying that
shouldDehydrateQuery by default has something else inside that I need to copy over?
Ah yeah I see "The default version only includes successful queries, do shouldDehydrateQuery: () => true to include all queries"
Looks like I need to add query.state.status === 'success'. I wonder if it would be useful to link to in the docsrival-blackโข3y ago
yeah that's the default implementation:
https://github.com/TanStack/query/blob/357ec041a6fcc4a550f3df02c12ecc7bcdefbc05/packages/query-core/src/hydration.ts#L72-L74
GitHub
query/hydration.ts at 357ec041a6fcc4a550f3df02c12ecc7bcdefbc05 ยท Ta...
๐ค Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query. - query/hydration.ts at 357ec041a6fcc4a5...
rival-blackโข3y ago
feel free to link it in the docs
fascinating-indigoOPโข3y ago
I can update the docs! Do you think it would be valuable to just export the function to be used by people? I.e.
return isSortable && defaultShouldDehydrateQuery() where defaultShouldDehydrateQuery is imported from react-query?rival-blackโข3y ago
yeah we can do that
secure-lavenderโข3y ago
๐
And do you think having a codesandbox example be worth it? To illustrate one implementation ?
fascinating-indigoOPโข3y ago
Awesome, I can make a PR ๐
secure-lavenderโข3y ago
Could contribute if needed
fascinating-indigoOPโข3y ago
https://github.com/TanStack/query/pull/4751
Draft PR up, Im not sure if I need to write tests or anything but feedback welcome!
rival-blackโข3y ago
looks good - prettier is failing though ๐
fascinating-indigoOPโข3y ago
My bad, pushed a fix
Any idea why the test is failing?
rival-blackโข3y ago
sorry for that, it's random test flakiness that I didn't have time to get around to yet
One thing that bothers me a tiny bit is the bundle size increase in this PR, presumably because
defaultShouldDehydrateQuery was local and could be minified before, but now it can't because it's a public interface ๐คfascinating-indigoOPโข3y ago
Whats the size increase? I was looking in the test for the size and im not sure what the diff is
rival-blackโข3y ago
It doesn't show a diff, only an absolute size. It's just 0.02kb larger than main. Maybe we can rename those functions ?