Open History to see the last commit
1---
2id: createSyncStoragePersister
3title: createSyncStoragePersister
4---
5β
6## Installation
7β
8This utility comes as a separate package and is available under the `'@tanstack/query-sync-storage-persister'` import.
9```bash
10npm install @tanstack/query-sync-storage-persister @tanstack/react-query-persist-client
11```
12or
13```bash
14pnpm add @tanstack/query-sync-storage-persister @tanstack/react-query-persist-client
15```
16or
17```bash
18yarn add @tanstack/query-sync-storage-persister @tanstack/react-query-persist-client
19```
20β
21## Usage
22β
23- Import the `createSyncStoragePersister` function
24- Create a new syncStoragePersister
25- Pass it to the [`persistQueryClient`](../plugins/persistQueryClient) function
26β
27```tsx
28import { persistQueryClient } from '@tanstack/react-query-persist-client'
29import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister'
30β
31const queryClient = new QueryClient({
32 defaultOptions: {
33 queries: {
34 gcTime: 1000 * 60 * 60 * 24, // 24 hours
35 },
36 },
37})
38β
39const localStoragePersister = createSyncStoragePersister({ storage: window.localStorage })
40// const sessionStoragePersister = createSyncStoragePersister({ storage: window.sessionStorage })
41β
42persistQueryClient({
43 queryClient,
44 persister: localStoragePersister,
45})
46```
47β
48## Retries
49β
50Persistence can fail, e.g. if the size exceeds the available space on the storage. Errors can be handled gracefully by providing a `retry` function to the persister.
51β
52The retry function receives the `persistedClient` it tried to save, as well as the `error` and the `errorCount` as input. It is expected to return a _new_ `PersistedClient`, with which it tries to persist again. If _undefined_ is returned, there will be no further attempt to persist.
53β
54```tsx
55export type PersistRetryer = (props: {
56 persistedClient: PersistedClient
57 error: Error
58 errorCount: number
59}) => PersistedClient | undefined
60```
61β
62### Predefined strategies
63β
64Per default, no retry will occur. You can use one of the predefined strategies to handle retries. They can be imported `from '@tanstack/react-query-persist-client'`:
65β
66- `removeOldestQuery`
67 - will return a new `PersistedClient` with the oldest query removed.
68β
69```tsx
70const localStoragePersister = createSyncStoragePersister({
71 storage: window.localStorage,
72 retry: removeOldestQuery
73})
74```
75β
76## API
77β
78### `createSyncStoragePersister`
79β
80Call this function to create a syncStoragePersister that you can use later with `persistQueryClient`.
81β
82```tsx
83createSyncStoragePersister(options: CreateSyncStoragePersisterOptions)
84```
85β
86### `Options`
87β
88```tsx
89interface CreateSyncStoragePersisterOptions {
90 /** The storage client used for setting an retrieving items from cache (window.localStorage or window.sessionStorage) */
91 storage: Storage | undefined | null
92 /** The key to use when storing the cache */
93 key?: string
94 /** To avoid spamming,
95 * pass a time in ms to throttle saving the cache to disk */
96 throttleTime?: number
97 /** How to serialize the data to storage */
98 serialize?: (client: PersistedClient) => string
99 /** How to deserialize the data from storage */
100 deserialize?: (cachedString: string) => PersistedClient
101 /** How to retry persistence on error **/
102 retry?: PersistRetryer
103}
104```
105β
106The default options are:
107β
108```tsx
109{
110 key = `REACT_QUERY_OFFLINE_CACHE`,
111 throttleTime = 1000,
112 serialize = JSON.stringify,
113 deserialize = JSON.parse,
114}
115```
116β
117#### `serialize` and `deserialize` options
118There is a limit to the amount of data which can be stored in `localStorage`.
119If you need to store more data in `localStorage`, you can override the `serialize` and `deserialize` functions to compress and decrompress the data using a library like [lz-string](https://github.com/pieroxy/lz-string/).
120β
121```tsx
122import { QueryClient } from '@tanstack/react-query';
123import { persistQueryClient } from '@tanstack/react-query-persist-client'
124import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister'
125β
126import { compress, decompress } from 'lz-string';
127β
128const queryClient = new QueryClient({ defaultOptions: { queries: { staleTime: Infinity } } });
129β
130persistQueryClient({
131 queryClient: queryClient,
132 persister: createSyncStoragePersister({
133 storage: window.localStorage,
134 serialize: data => compress(JSON.stringify(data)),
135 deserialize: data => JSON.parse(decompress(data)),
136 }),
137 maxAge: Infinity,
138});
139```