IndexedDb Persister #3198
-
Now that we have better persister in react-query v4 alpha 🚀, I was also going to see about sharing my IndexedDb persister. It's super easy: import { get, set, del } from "idb-keyval";
import { PersistedClient, Persister } from "react-query/persistQueryClient";
/**
* Creates an Indexed DB persister
* @see https://react-query.tanstack.com/plugins/persistQueryClient#building-a-persistor
* @see https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API
*/
export function createIDBPersister(idbValidKey: IDBValidKey = "reactQuery") {
return {
persistClient: async (client: PersistedClient) => {
set(idbValidKey, client);
},
restoreClient: async () => {
return await get<PersistedClient>(idbValidKey);
},
removeClient: async () => {
await del(idbValidKey);
},
} as Persister;
} Benefits
Shall I submit as a PR?I can also add documentation and examples for use. The only issue is dependencies. This requires idb-keyval and |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 26 replies
-
Nice! Using the vanilla IndexDB API's are not a great experience so glad that IDB keyval exists; it definitely makes the implementation easier. If idb-keyval is used for the IndexDB Persister, it should be included as a peerDependency like how
I think having the option to use
This is no limit is nice. I've hit the limit 5MB limit very easily with localStorage, due to 3rd party libraries stuffing data in the localStorage for their own requirements, leaving less space for my app data Some follow up questions are:Are there any downsides/alternatives to including
|
Beta Was this translation helpful? Give feedback.
-
isn't this just an async storage persister with a bit of renaming?
you would also profit from throttling. While we've made the this is the interface that an |
Beta Was this translation helpful? Give feedback.
-
merged in PR: #3356 |
Beta Was this translation helpful? Give feedback.
-
Thank you for the insight! This is a way around the 5MB limit for |
Beta Was this translation helpful? Give feedback.
-
feature request: in my case, restoreClient takes 0.5 seconds, related: XY problem issue in #4368 this would help: a global setting draft ...
onSubscribe() {
if (this.listeners.length === 1) {
this.currentQuery.addObserver(this);
if (shouldFetchOnMount(this.currentQuery, this.options)) {
//this.executeFetch();
const queryClient = this.client
if (!queryClient.paused) {
this.executeFetch();
}
else {
// queryClient is paused -> fetch later
if (!queryClient.afterPersistQueryClientRestore) {
queryClient.afterPersistQueryClientRestore = []
}
queryClient.afterPersistQueryClientRestore.push(() => {
if (shouldFetchOnMount(this.currentQuery, this.options)) {
this.executeFetch();
}
})
}
}
this.updateTimers();
}
}
async function persistQueryClientRestore({
queryClient,
persister,
maxAge = 1000 * 60 * 60 * 24,
buster = '',
hydrateOptions
}) {
queryClient.paused = true
try {
// ...
} catch (err) {
// ...
}
queryClient.paused = false
if (queryClient.afterPersistQueryClientRestore) {
for (const fn of queryClient.afterPersistQueryClientRestore) {
fn()
}
}
} |
Beta Was this translation helpful? Give feedback.
merged in PR: #3356