feat: add idb persister to cache requests for 24 hours

This commit is contained in:
2024-04-20 22:58:26 +02:00
parent 730090ca9e
commit 04ec9f21cf
5 changed files with 69 additions and 8 deletions

View File

@@ -0,0 +1,22 @@
import { get, set, del } from "idb-keyval";
import {
PersistedClient,
Persister,
} from "@tanstack/react-query-persist-client";
/**
* Creates an Indexed DB persister
* @see https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API
*/
export function createIDBPersister(idbValidKey: IDBValidKey = "reactQuery") {
return {
persistClient: async (client: PersistedClient) => {
await set(idbValidKey, client);
},
restoreClient: async () => {
return await get<PersistedClient>(idbValidKey);
},
removeClient: async () => {
await del(idbValidKey);
},
} as Persister;
}