Open History to see the last commit
1---
2id: persistQueryClient
3title: persistQueryClient (Experimental)
4---
5β
6> VERY IMPORTANT: This utility is currently in an experimental stage. This means that breaking changes will happen in minor AND patch releases. Use at your own risk. If you choose to rely on this in production in an experimental stage, please lock your version to a patch-level version to avoid unexpected breakages.
7β
8`persistQueryClient` is a utility for persisting the state of your queryClient and its caches for later use. Different **persistors** can be used to store your client and cache to many different storage layers.
9β
10## Officially Supported Persistors
11β
12- [createLocalStoragePersistor (Experimental)](../createLocalStoragePersistor)
13β
14## Installation
15β
16This utility comes packaged with `react-query` and is available under the `react-query/persistQueryClient-experimental` import.
17β
18## Usage
19β
20Import the `persistQueryClient` function, and pass it your `QueryClient` instance (with a `cacheTime` set), and a Persistor interface (there are multiple persistor types you can use):
21β
22```ts
23import { persistQueryClient } from 'react-query/persistQueryClient-experimental'
24import { createLocalStoragePersistor } from 'react-query/createLocalStoragePersistor-experimental'
25β
26const queryClient = new QueryClient({
27 defaultOptions: {
28 queries: {
29 cacheTime: 1000 * 60 * 60 * 24, // 24 hours
30 },
31 },
32})
33β
34const localStoragePersistor = createLocalStoragePersistor()
35β
36persistQueryClient({
37 queryClient
38 persistor: localStoragePersistor,
39})
40```
41β
42**IMPORTANT** - for persist to work properly, you need to pass `QueryClient` a `cacheTime` value to override the default during hydration (as shown above).
43β
44If it is not set when creating the `QueryClient` instance, it will default to `300000` (5 minutes) for hydration, and the stored cache will be discarded after 5 minutes of inactivity. This is the default garbage collection behavior.
45β
46It should be set as the same value or higher than persistQueryClient's `maxAge` option. E.g. if `maxAge` is 24 hours (the default) then `cacheTime` should be 24 hours or higher. If lower than `maxAge`, garbage collection will kick in and discard the stored cache earlier than expected.
47β
48You can also pass it `Infinity` to disable garbage collection behavior entirely.
49β
50## How does it work?
51β
52As you use your application:
53β
54- When your query/mutation cache is updated, it will be dehydrated and stored by the persistor you provided. **By default**, this action is throttled to happen at most every 1 second to save on potentially expensive writes to a persistor, but can be customized as you see fit.
55β
56When you reload/bootstrap your app:
57β
58- Attempts to load a previously persisted dehydrated query/mutation cache from the persistor
59- If a cache is found that is older than the `maxAge` (which by default is 24 hours), it will be discarded. This can be customized as you see fit.
60β
61## Cache Busting
62β
63Sometimes you may make changes to your application or data that immediately invalidate any and all cached data. If and when this happens, you can pass a `buster` string option to `persistQueryClient`, and if the cache that is found does not also have that buster string, it will be discarded.
64β
65```ts
66persistQueryClient({ queryClient, persistor, buster: buildHash })
67```
68β
69## API
70β
71### `persistQueryClient`
72β
73Pass this function a `QueryClient` instance and a persistor that will persist your cache. Both are **required**
74β
75```ts
76persistQueryClient({ queryClient, persistor })
77```
78β
79### `Options`
80β
81An object of options:
82β
83```ts
84interface PersistQueryClientOptions {
85 /** The QueryClient to persist */
86 queryClient: QueryClient
87 /** The Persistor interface for storing and restoring the cache
88 * to/from a persisted location */
89 persistor: Persistor
90 /** The max-allowed age of the cache.
91 * If a persisted cache is found that is older than this
92 * time, it will be discarded */
93 maxAge?: number
94 /** A unique string that can be used to forcefully
95 * invalidate existing caches if they do not share the same buster string */
96 buster?: string
97}
98```
99β
100The default options are:
101β
102```ts
103{
104 maxAge = 1000 * 60 * 60 * 24, // 24 hours
105 buster = '',
106}
107```
108β
109## Building a Persistor
110β
111Persistors have the following interface:
112β
113```ts
114export interface Persistor {
115 persistClient(persistClient: PersistedClient): Promisable<void>
116 restoreClient(): Promisable<PersistedClient | undefined>
117 removeClient(): Promisable<void>
118}
119```
120β
121Persisted Client entries have the following interface:
122β
123```ts
124export interface PersistedClient {
125 timestamp: number
126 buster: string
127 cacheState: any
128}
129```
130β
131Satisfy all of these interfaces and you've got yourself a persistor!