Skip to content

Commit be6bfbb

Browse files
authored
feat: session storage persistor (TanStack#2282)
LocalStoragePersistor is now integrated with WebStoragePersistor, which can take any `Storage`
1 parent 98dd91f commit be6bfbb

File tree

10 files changed

+50
-44
lines changed

10 files changed

+50
-44
lines changed

Diff for: createLocalStoragePersistor-experimental/package.json

-6
This file was deleted.

Diff for: createWebStoragePersistor-experimental/package.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"internal": true,
3+
"main": "../lib/createWebStoragePersistor-experimental/index.js",
4+
"module": "../es/createWebStoragePersistor-experimental/index.js",
5+
"types": "../types/createWebStoragePersistor-experimental/index.d.ts"
6+
}

Diff for: docs/src/manifests/manifest.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,9 @@
308308
"editUrl": "/plugins/persistQueryClient.md"
309309
},
310310
{
311-
"title": "createLocalStoragePersistor (Experimental)",
312-
"path": "/plugins/createLocalStoragePersistor",
313-
"editUrl": "/plugins/createLocalStoragePersistor.md"
311+
"title": "createWebStoragePersistor (Experimental)",
312+
"path": "/plugins/createWebStoragePersistor",
313+
"editUrl": "/plugins/createWebStoragePersistor.md"
314314
},
315315
{
316316
"title": "broadcastQueryClient (Experimental)",

Diff for: docs/src/pages/guides/migrating-to-react-query-3.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Previous versions of React Query were awesome and brought some amazing new featu
2020
- Observe queries/mutations outside of React
2121
- Use the React Query core logic anywhere you want!
2222
- Bundled/Colocated Devtools via `react-query/devtools`
23-
- Cache Persistence to localstorage (experimental via `react-query/persistQueryClient-experimental` and `react-query/createLocalStoragePersistor-experimental`)
23+
- Cache Persistence to web storage (experimental via `react-query/persistQueryClient-experimental` and `react-query/createWebStoragePersistor-experimental`)
2424

2525
## Breaking Changes
2626

Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
---
2-
id: createLocalStoragePersistor
3-
title: createLocalStoragePersistor (Experimental)
2+
id: createWebStoragePersistor
3+
title: createWebStoragePersistor (Experimental)
44
---
55

66
> 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.
77
88
## Installation
99

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

1212
## Usage
1313

14-
- Import the `createLocalStoragePersistor` function
15-
- Create a new localStoragePersistor
14+
- Import the `createWebStoragePersistor` function
15+
- Create a new webStoragePersistor
1616
- Pass it to the [`persistQueryClient`](../persistQueryClient) function
1717

1818
```ts
1919
import { persistQueryClient } from 'react-query/persistQueryClient-experimental'
20-
import { createLocalStoragePersistor } from 'react-query/createLocalStoragePersistor-experimental'
20+
import { createWebStoragePersistor } from 'react-query/createWebStoragePersistor-experimental'
2121

2222
const queryClient = new QueryClient({
2323
defaultOptions: {
@@ -27,7 +27,8 @@ const queryClient = new QueryClient({
2727
},
2828
})
2929

30-
const localStoragePersistor = createLocalStoragePersistor()
30+
const localStoragePersistor = createWebStoragePersistor({storage: window.localStorage})
31+
// const sessionStoragePersistor = createWebStoragePersistor({storage: window.sessionStorage})
3132

3233
persistQueryClient({
3334
queryClient,
@@ -37,23 +38,25 @@ persistQueryClient({
3738

3839
## API
3940

40-
### `createLocalStoragePersistor`
41+
### `createWebStoragePersistor`
4142

42-
Call this function (with an optional options object) to create a localStoragePersistor that you can use later with `persisteQueryClient`.
43+
Call this function (with an optional options object) to create a webStoragePersistor that you can use later with `persistQueryClient`.
4344

4445
```js
45-
createLocalStoragePersistor(options?: CreateLocalStoragePersistorOptions)
46+
createWebStoragePersistor(options?: CreateWebStoragePersistorOptions)
4647
```
4748

4849
### `Options`
4950

5051
An optional object of options:
5152

5253
```js
53-
interface CreateLocalStoragePersistorOptions {
54-
/** The key to use when storing the cache to localstorage */
55-
localStorageKey?: string
56-
/** To avoid localstorage spamming,
54+
interface CreateWebStoragePersistorOptions {
55+
/** The storage client used for setting an retrieving items from cache (window.localStorage or window.sessionStorage) */
56+
storage: Storage
57+
/** The key to use when storing the cache */
58+
key?: string
59+
/** To avoid spamming,
5760
* pass a time in ms to throttle saving the cache to disk */
5861
throttleTime?: number
5962
}
@@ -63,7 +66,7 @@ The default options are:
6366

6467
```js
6568
{
66-
localStorageKey = `REACT_QUERY_OFFLINE_CACHE`,
69+
key = `REACT_QUERY_OFFLINE_CACHE`,
6770
throttleTime = 1000,
6871
}
6972
```

Diff for: docs/src/pages/plugins/persistQueryClient.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ title: persistQueryClient (Experimental)
99

1010
## Officially Supported Persistors
1111

12-
- [createLocalStoragePersistor (Experimental)](/plugins/createLocalStoragePersistor)
12+
- [createWebStoragePersistor (Experimental)](/plugins/createWebStoragePersistor)
1313

1414
## Installation
1515

@@ -21,7 +21,7 @@ Import the `persistQueryClient` function, and pass it your `QueryClient` instanc
2121

2222
```ts
2323
import { persistQueryClient } from 'react-query/persistQueryClient-experimental'
24-
import { createLocalStoragePersistor } from 'react-query/createLocalStoragePersistor-experimental'
24+
import { createWebStoragePersistor } from 'react-query/createWebStoragePersistor-experimental'
2525

2626
const queryClient = new QueryClient({
2727
defaultOptions: {
@@ -31,7 +31,7 @@ const queryClient = new QueryClient({
3131
},
3232
})
3333

34-
const localStoragePersistor = createLocalStoragePersistor()
34+
const localStoragePersistor = createWebStoragePersistor({storage: window.localStorage})
3535

3636
persistQueryClient({
3737
queryClient,

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"hydration",
5252
"devtools",
5353
"persistQueryClient-experimental",
54-
"createLocalStoragePersistor-experimental",
54+
"createWebStoragePersistor-experimental",
5555
"broadcastQueryClient-experimental",
5656
"lib",
5757
"react",

Diff for: rollup.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ const inputSrcs = [
2626
'persistQueryClient-experimental',
2727
],
2828
[
29-
'src/createLocalStoragePersistor-experimental/index.ts',
30-
'ReactQueryCreateLocalStoragePersistorExperimental',
31-
'createLocalStoragePersistor-experimental',
29+
'src/createWebStoragePersistor-experimental/index.ts',
30+
'ReactQueryCreateWebStoragePersistorExperimental',
31+
'createWebStoragePersistor-experimental',
3232
],
3333
[
3434
'src/broadcastQueryClient-experimental/index.ts',

Diff for: src/createLocalStoragePersistor-experimental/index.ts renamed to src/createWebStoragePersistor-experimental/index.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import { noop } from '../core/utils'
22
import { PersistedClient, Persistor } from '../persistQueryClient-experimental'
33

4-
interface CreateLocalStoragePersistorOptions {
5-
/** The key to use when storing the cache to localstorage */
6-
localStorageKey?: string
7-
/** To avoid localstorage spamming,
4+
interface CreateWebStoragePersistorOptions {
5+
/** The storage client used for setting an retrieving items from cache */
6+
storage: Storage
7+
/** The key to use when storing the cache */
8+
key?: string
9+
/** To avoid spamming,
810
* pass a time in ms to throttle saving the cache to disk */
911
throttleTime?: number
1012
}
1113

12-
export function createLocalStoragePersistor({
13-
localStorageKey = `REACT_QUERY_OFFLINE_CACHE`,
14+
export function createWebStoragePersistor({
15+
storage,
16+
key = `REACT_QUERY_OFFLINE_CACHE`,
1417
throttleTime = 1000,
15-
}: CreateLocalStoragePersistorOptions = {}): Persistor {
16-
if (typeof localStorage !== 'undefined') {
18+
}: CreateWebStoragePersistorOptions): Persistor {
19+
if (typeof storage !== 'undefined') {
1720
return {
1821
persistClient: throttle(persistedClient => {
19-
localStorage.setItem(localStorageKey, JSON.stringify(persistedClient))
22+
storage.setItem(key, JSON.stringify(persistedClient))
2023
}, throttleTime),
2124
restoreClient: () => {
22-
const cacheString = localStorage.getItem(localStorageKey)
25+
const cacheString = storage.getItem(key)
2326

2427
if (!cacheString) {
2528
return
@@ -28,7 +31,7 @@ export function createLocalStoragePersistor({
2831
return JSON.parse(cacheString) as PersistedClient
2932
},
3033
removeClient: () => {
31-
localStorage.removeItem(localStorageKey)
34+
storage.removeItem(key)
3235
},
3336
}
3437
}

Diff for: tsconfig.types.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"./src/hydration/index.ts",
1515
"./src/devtools/index.ts",
1616
"./src/persistQueryClient-experimental/index.ts",
17-
"./src/createLocalStoragePersistor-experimental/index.ts",
17+
"./src/createWebStoragePersistor-experimental/index.ts",
1818
"./src/broadcastQueryClient-experimental/index.ts"
1919
],
2020
"exclude": ["./src/**/*"]

0 commit comments

Comments
 (0)