Skip to content

Commit

Permalink
docs: Document compression use case for createWebStoragePersister (#…
Browse files Browse the repository at this point in the history
…3285)

Add an example on how to `compress`/`decompress` data from local storage in case you need to cache large payloads.

Context: #2864 (comment)
  • Loading branch information
hverlin authored Feb 10, 2022
1 parent 6ec6811 commit 7064c69
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/src/pages/plugins/createWebStoragePersister.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,27 @@ The default options are:
deserialize = JSON.parse,
}
```

#### `serialize` and `deserialize` options
There is a limit to the amount of data which can be stored in `localStorage`.
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/).

```js
import { QueryClient } from 'react-query';
import { persistQueryClient } from 'react-query/persistQueryClient'
import { createWebStoragePersister } from 'react-query/createWebStoragePersister'

import { compress, decompress } from 'lz-string';

const queryClient = new QueryClient({ defaultOptions: { queries: { staleTime: Infinity } } });

persistQueryClient({
queryClient: connectionsQueryClient,
persistor: createWebStoragePersister({
storage: window.localStorage,
serialize: data => compress(JSON.stringify(data)),
deserialize: data => JSON.parse(decompress(data)),
}),
maxAge: Infinity,
});
```

0 comments on commit 7064c69

Please sign in to comment.