Skip to content

Commit 7064c69

Browse files
authored
docs: Document compression use case for createWebStoragePersister (#3285)
Add an example on how to `compress`/`decompress` data from local storage in case you need to cache large payloads. Context: #2864 (comment)
1 parent 6ec6811 commit 7064c69

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

docs/src/pages/plugins/createWebStoragePersister.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,27 @@ The default options are:
7272
deserialize = JSON.parse,
7373
}
7474
```
75+
76+
#### `serialize` and `deserialize` options
77+
There is a limit to the amount of data which can be stored in `localStorage`.
78+
If 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/).
79+
80+
```js
81+
import { QueryClient } from 'react-query';
82+
import { persistQueryClient } from 'react-query/persistQueryClient'
83+
import { createWebStoragePersister } from 'react-query/createWebStoragePersister'
84+
85+
import { compress, decompress } from 'lz-string';
86+
87+
const queryClient = new QueryClient({ defaultOptions: { queries: { staleTime: Infinity } } });
88+
89+
persistQueryClient({
90+
queryClient: connectionsQueryClient,
91+
persistor: createWebStoragePersister({
92+
storage: window.localStorage,
93+
serialize: data => compress(JSON.stringify(data)),
94+
deserialize: data => JSON.parse(decompress(data)),
95+
}),
96+
maxAge: Infinity,
97+
});
98+
```

0 commit comments

Comments
 (0)