Skip to content

Commit fa12391

Browse files
prateek3255TkDodo
andauthored
refactor(persistQueryClient): Make persistQueryClient stable (#2961)
* 🚚 Remove experimental from persist-query-client * 🚚 Rename persistor -> persister * ✏️ Fix Persistor -> Persister in imports * 🚚 Update name in rollup config * 🚚 Move createAsyncStoragePersister and createWebStoragePersister to stable version and rename persistor -> persister * 📝 Update documentation * 📝 Add migrating to v4 docs * Apply suggestions from code review Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
1 parent ac1eefd commit fa12391

File tree

19 files changed

+151
-126
lines changed

19 files changed

+151
-126
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"internal": true,
3+
"main": "../lib/createAsyncStoragePersister/index.js",
4+
"module": "../es/createAsyncStoragePersister/index.js",
5+
"types": "../types/createAsyncStoragePersister/index.d.ts"
6+
}

createAsyncStoragePersistor-experimental/package.json

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"internal": true,
3+
"main": "../lib/createWebStoragePersister/index.js",
4+
"module": "../es/createWebStoragePersister/index.js",
5+
"types": "../types/createWebStoragePersister/index.d.ts"
6+
}

createWebStoragePersistor-experimental/package.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

docs/src/manifests/manifest.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,19 +319,19 @@
319319
"heading": true,
320320
"routes": [
321321
{
322-
"title": "persistQueryClient (Experimental)",
322+
"title": "persistQueryClient",
323323
"path": "/plugins/persistQueryClient",
324324
"editUrl": "/plugins/persistQueryClient.md"
325325
},
326326
{
327-
"title": "createWebStoragePersistor (Experimental)",
328-
"path": "/plugins/createWebStoragePersistor",
329-
"editUrl": "/plugins/createWebStoragePersistor.md"
327+
"title": "createWebStoragePersister",
328+
"path": "/plugins/createWebStoragePersister",
329+
"editUrl": "/plugins/createWebStoragePersister.md"
330330
},
331331
{
332-
"title": "createAsyncStoragePersistor (Experimental)",
333-
"path": "/plugins/createAsyncStoragePersistor",
334-
"editUrl": "/plugins/createAsyncStoragePersistor.md"
332+
"title": "createAsyncStoragePersister",
333+
"path": "/plugins/createAsyncStoragePersister",
334+
"editUrl": "/plugins/createAsyncStoragePersister.md"
335335
},
336336
{
337337
"title": "broadcastQueryClient (Experimental)",

docs/src/pages/guides/migrating-to-react-query-4.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,22 @@ If you were importing anything from `'react-query/react'` directly in your proje
132132
+ import { QueryClientProvider } from 'react-query/reactjs';
133133
```
134134

135+
### `persistQueryClient` and the corresponding persister plugins are no longer experimental and have been renamed
136+
137+
The plugins `createWebStoragePersistor` and `createAsyncStoragePersistor` have been renamed to [`createWebStoragePersister`](/plugins/createWebStoragePersister) and [`createAsyncStoragePersister`](/plugins/createAsyncStoragePersister) respectively. The interface `Persistor` in `persistQueryClient` has also been renamed to `Persister`. Checkout [this stackexchange](https://english.stackexchange.com/questions/206893/persister-or-persistor) for the motivation of this change.
138+
139+
Since these plugins are no longer experimental, their import paths have also been updated:
140+
141+
```diff
142+
- import { persistQueryClient } from 'react-query/persistQueryClient-experimental'
143+
- import { createWebStoragePersistor } from 'react-query/createWebStoragePersistor-experimental'
144+
- import { createAsyncStoragePersistor } from 'react-query/createAsyncStoragePersistor-experimental'
145+
146+
+ import { persistQueryClient } from 'react-query/persistQueryClient'
147+
+ import { createWebStoragePersister } from 'react-query/createWebStoragePersister'
148+
+ import { createAsyncStoragePersister } from 'react-query/createAsyncStoragePersister'
149+
```
150+
135151
## New Features 🚀
136152

137153
### Mutation Cache Garbage Collection

docs/src/pages/plugins/createAsyncStoragePersistor.md renamed to docs/src/pages/plugins/createAsyncStoragePersister.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
---
2-
id: createAsyncStoragePersistor
3-
title: createAsyncStoragePersistor (Experimental)
2+
id: createAsyncStoragePersister
3+
title: createAsyncStoragePersister
44
---
55

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-
86
## Installation
97

10-
This utility comes packaged with `react-query` and is available under the `react-query/createAsyncStoragePersistor-experimental` import.
8+
This utility comes packaged with `react-query` and is available under the `react-query/createAsyncStoragePersister` import.
119

1210
## Usage
1311

14-
- Import the `createAsyncStoragePersistor` function
15-
- Create a new asyncStoragePersistor
12+
- Import the `createAsyncStoragePersister` function
13+
- Create a new asyncStoragePersister
1614
- you can pass any `storage` to it that adheres to the `AsyncStorage` interface - the example below uses the async-storage from React Native
1715
- Pass it to the [`persistQueryClient`](./persistQueryClient) function
1816

1917
```ts
2018
import AsyncStorage from '@react-native-async-storage/async-storage'
21-
import { persistQueryClient } from 'react-query/persistQueryClient-experimental'
22-
import { createAsyncStoragePersistor } from 'react-query/createAsyncStoragePersistor-experimental'
19+
import { persistQueryClient } from 'react-query/persistQueryClient'
20+
import { createAsyncStoragePersister } from 'react-query/createAsyncStoragePersister'
2321

2422
const queryClient = new QueryClient({
2523
defaultOptions: {
@@ -29,30 +27,30 @@ const queryClient = new QueryClient({
2927
},
3028
})
3129

32-
const asyncStoragePersistor = createAsyncStoragePersistor({
30+
const asyncStoragePersister = createAsyncStoragePersister({
3331
storage: AsyncStorage
3432
})
3533

3634
persistQueryClient({
3735
queryClient,
38-
persistor: asyncStoragePersistor,
36+
persister: asyncStoragePersister,
3937
})
4038
```
4139

4240
## API
4341

44-
### `createAsyncStoragePersistor`
42+
### `createAsyncStoragePersister`
4543

46-
Call this function to create an asyncStoragePersistor that you can use later with `persistQueryClient`.
44+
Call this function to create an asyncStoragePersister that you can use later with `persistQueryClient`.
4745

4846
```js
49-
createAsyncStoragePersistor(options: CreateAsyncStoragePersistorOptions)
47+
createAsyncStoragePersister(options: CreateAsyncStoragePersisterOptions)
5048
```
5149

5250
### `Options`
5351

5452
```ts
55-
interface CreateAsyncStoragePersistorOptions {
53+
interface CreateAsyncStoragePersisterOptions {
5654
/** The storage client used for setting an retrieving items from cache */
5755
storage: AsyncStorage
5856
/** The key to use when storing the cache to localStorage */

docs/src/pages/plugins/createWebStoragePersistor.md renamed to docs/src/pages/plugins/createWebStoragePersister.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
---
2-
id: createWebStoragePersistor
3-
title: createWebStoragePersistor (Experimental)
2+
id: createWebStoragePersister
3+
title: createWebStoragePersister
44
---
55

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-
86
## Installation
97

10-
This utility comes packaged with `react-query` and is available under the `react-query/createWebStoragePersistor-experimental` import.
8+
This utility comes packaged with `react-query` and is available under the `react-query/createWebStoragePersister` import.
119

1210
## Usage
1311

14-
- Import the `createWebStoragePersistor` function
15-
- Create a new webStoragePersistor
12+
- Import the `createWebStoragePersister` function
13+
- Create a new webStoragePersister
1614
- Pass it to the [`persistQueryClient`](./persistQueryClient) function
1715

1816
```ts
19-
import { persistQueryClient } from 'react-query/persistQueryClient-experimental'
20-
import { createWebStoragePersistor } from 'react-query/createWebStoragePersistor-experimental'
17+
import { persistQueryClient } from 'react-query/persistQueryClient'
18+
import { createWebStoragePersister } from 'react-query/createWebStoragePersister'
2119

2220
const queryClient = new QueryClient({
2321
defaultOptions: {
@@ -27,29 +25,29 @@ const queryClient = new QueryClient({
2725
},
2826
})
2927

30-
const localStoragePersistor = createWebStoragePersistor({ storage: window.localStorage })
31-
// const sessionStoragePersistor = createWebStoragePersistor({ storage: window.sessionStorage })
28+
const localStoragePersister = createWebStoragePersister({ storage: window.localStorage })
29+
// const sessionStoragePersister = createWebStoragePersister({ storage: window.sessionStorage })
3230

3331
persistQueryClient({
3432
queryClient,
35-
persistor: localStoragePersistor,
33+
persister: localStoragePersister,
3634
})
3735
```
3836

3937
## API
4038

41-
### `createWebStoragePersistor`
39+
### `createWebStoragePersister`
4240

43-
Call this function to create a webStoragePersistor that you can use later with `persistQueryClient`.
41+
Call this function to create a webStoragePersister that you can use later with `persistQueryClient`.
4442

4543
```js
46-
createWebStoragePersistor(options: CreateWebStoragePersistorOptions)
44+
createWebStoragePersister(options: CreateWebStoragePersisterOptions)
4745
```
4846

4947
### `Options`
5048

5149
```ts
52-
interface CreateWebStoragePersistorOptions {
50+
interface CreateWebStoragePersisterOptions {
5351
/** The storage client used for setting an retrieving items from cache (window.localStorage or window.sessionStorage) */
5452
storage: Storage
5553
/** The key to use when storing the cache */

docs/src/pages/plugins/persistQueryClient.md

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
---
22
id: persistQueryClient
3-
title: persistQueryClient (Experimental)
3+
title: persistQueryClient
44
---
55

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.
6+
`persistQueryClient` is a utility for persisting the state of your queryClient and its caches for later use. Different **persisters** can be used to store your client and cache to many different storage layers.
77

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.
8+
## Officially Supported Persisters
99

10-
## Officially Supported Persistors
11-
12-
- [createWebStoragePersistor (Experimental)](/plugins/createWebStoragePersistor)
13-
- [createAsyncStoragePersistor (Experimental)](/plugins/createAsyncStoragePersistor)
10+
- [createWebStoragePersister](/plugins/createWebStoragePersister)
11+
- [createAsyncStoragePersister](/plugins/createAsyncStoragePersister)
1412

1513
## Installation
1614

17-
This utility comes packaged with `react-query` and is available under the `react-query/persistQueryClient-experimental` import.
15+
This utility comes packaged with `react-query` and is available under the `react-query/persistQueryClient` import.
1816

1917
## Usage
2018

21-
Import 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):
19+
Import the `persistQueryClient` function, and pass it your `QueryClient` instance (with a `cacheTime` set), and a Persister interface (there are multiple persister types you can use):
2220

2321
```ts
24-
import { persistQueryClient } from 'react-query/persistQueryClient-experimental'
25-
import { createWebStoragePersistor } from 'react-query/createWebStoragePersistor-experimental'
22+
import { persistQueryClient } from 'react-query/persistQueryClient'
23+
import { createWebStoragePersister } from 'react-query/createWebStoragePersister'
2624

2725
const queryClient = new QueryClient({
2826
defaultOptions: {
@@ -32,11 +30,11 @@ const queryClient = new QueryClient({
3230
},
3331
})
3432

35-
const localStoragePersistor = createWebStoragePersistor({storage: window.localStorage})
33+
const localStoragePersister = createWebStoragePersister({storage: window.localStorage})
3634

3735
persistQueryClient({
3836
queryClient,
39-
persistor: localStoragePersistor,
37+
persister: localStoragePersister,
4038
})
4139
```
4240

@@ -52,29 +50,29 @@ You can also pass it `Infinity` to disable garbage collection behavior entirely.
5250

5351
As you use your application:
5452

55-
- 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.
53+
- When your query/mutation cache is updated, it will be dehydrated and stored by the persister you provided. **By default**, this action is throttled to happen at most every 1 second to save on potentially expensive writes to a persister, but can be customized as you see fit.
5654

5755
When you reload/bootstrap your app:
5856

59-
- Attempts to load a previously persisted dehydrated query/mutation cache from the persistor
57+
- Attempts to load a previously persisted dehydrated query/mutation cache from the persister
6058
- 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.
6159

6260
## Cache Busting
6361

6462
Sometimes 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.
6563

6664
```ts
67-
persistQueryClient({ queryClient, persistor, buster: buildHash })
65+
persistQueryClient({ queryClient, persister, buster: buildHash })
6866
```
6967

7068
## API
7169

7270
### `persistQueryClient`
7371

74-
Pass this function a `QueryClient` instance and a persistor that will persist your cache. Both are **required**
72+
Pass this function a `QueryClient` instance and a persister that will persist your cache. Both are **required**
7573

7674
```ts
77-
persistQueryClient({ queryClient, persistor })
75+
persistQueryClient({ queryClient, persister })
7876
```
7977

8078
### `Options`
@@ -85,9 +83,9 @@ An object of options:
8583
interface PersistQueryClientOptions {
8684
/** The QueryClient to persist */
8785
queryClient: QueryClient
88-
/** The Persistor interface for storing and restoring the cache
86+
/** The Persister interface for storing and restoring the cache
8987
* to/from a persisted location */
90-
persistor: Persistor
88+
persister: Persister
9189
/** The max-allowed age of the cache.
9290
* If a persisted cache is found that is older than this
9391
* time, it will be discarded */
@@ -111,12 +109,12 @@ The default options are:
111109
}
112110
```
113111

114-
## Building a Persistor
112+
## Building a Persister
115113

116-
Persistors have the following interface:
114+
Persisters have the following interface:
117115

118116
```ts
119-
export interface Persistor {
117+
export interface Persister {
120118
persistClient(persistClient: PersistedClient): Promisable<void>
121119
restoreClient(): Promisable<PersistedClient | undefined>
122120
removeClient(): Promisable<void>
@@ -133,4 +131,4 @@ export interface PersistedClient {
133131
}
134132
```
135133

136-
Satisfy all of these interfaces and you've got yourself a persistor!
134+
Satisfy all of these interfaces and you've got yourself a persister!

examples/basic/src/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,23 @@ import {
99
QueryClientProvider,
1010
} from "react-query";
1111
import { ReactQueryDevtools } from "react-query/devtools";
12+
import { persistQueryClient } from 'react-query/persistQueryClient';
13+
import { createWebStoragePersister } from 'react-query/createWebStoragePersister'
1214

13-
const queryClient = new QueryClient();
15+
const queryClient = new QueryClient({
16+
defaultOptions: {
17+
queries: {
18+
cacheTime: 1000 * 60 * 60 * 24, // 24 hours
19+
},
20+
},
21+
})
22+
23+
const localStoragePersister = createWebStoragePersister({storage: window.localStorage})
24+
25+
persistQueryClient({
26+
queryClient,
27+
persister: localStoragePersister,
28+
})
1429

1530
function App() {
1631
const [postId, setPostId] = React.useState(-1);

0 commit comments

Comments
 (0)