Skip to content

Commit 40832bb

Browse files
committed
feat: session storage persistor
1 parent afc6a57 commit 40832bb

File tree

9 files changed

+146
-0
lines changed

9 files changed

+146
-0
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/createSessionStoragePersistor-experimental/index.js",
4+
"module": "../es/createSessionStoragePersistor-experimental/index.js",
5+
"types": "../types/createSessionStoragePersistor-experimental/index.d.ts"
6+
}

docs/src/manifests/manifest.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@
312312
"path": "/plugins/createLocalStoragePersistor",
313313
"editUrl": "/plugins/createLocalStoragePersistor.md"
314314
},
315+
{
316+
"title": "createSessionStoragePersistor (Experimental)",
317+
"path": "/plugins/createSessionStoragePersistor",
318+
"editUrl": "/plugins/createSessionStoragePersistor.md"
319+
},
315320
{
316321
"title": "broadcastQueryClient (Experimental)",
317322
"path": "/plugins/broadcastQueryClient",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Previous versions of React Query were awesome and brought some amazing new featu
2121
- Use the React Query core logic anywhere you want!
2222
- Bundled/Colocated Devtools via `react-query/devtools`
2323
- Cache Persistence to localstorage (experimental via `react-query/persistQueryClient-experimental` and `react-query/createLocalStoragePersistor-experimental`)
24+
- Cache Persistence to sessionstorage (experimental via `react-query/persistQueryClient-experimental` and `react-query/createSessionStoragePersistor-experimental`)
2425

2526
## Breaking Changes
2627

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
id: createSessionStoragePersistor
3+
title: createSessionStoragePersistor (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+
## Installation
9+
10+
This utility comes packaged with `react-query` and is available under the `react-query/createSessionStoragePersistor-experimental` import.
11+
12+
## Usage
13+
14+
- Import the `createSessionStoragePersistor` function
15+
- Create a new sessionStoragePersistor
16+
- Pass it to the [`persistQueryClient`](../persistQueryClient) function
17+
18+
```ts
19+
import { persistQueryClient } from 'react-query/persistQueryClient-experimental'
20+
import { createSessionStoragePersistor } from 'react-query/createSessionStoragePersistor-experimental'
21+
22+
const queryClient = new QueryClient({
23+
defaultOptions: {
24+
queries: {
25+
cacheTime: 1000 * 60 * 60 * 24, // 24 hours
26+
},
27+
},
28+
})
29+
30+
const sessionStoragePersistor = createSessionStoragePersistor()
31+
32+
persistQueryClient({
33+
queryClient,
34+
persistor: sessionStoragePersistor,
35+
})
36+
```
37+
38+
## API
39+
40+
### `createSessionStoragePersistor`
41+
42+
Call this function (with an optional options object) to create a sessionStoragePersistor that you can use later with `persisteQueryClient`.
43+
44+
```js
45+
createSessionStoragePersistor(options?: CreateSessionStoragePersistorOptions)
46+
```
47+
48+
### `Options`
49+
50+
An optional object of options:
51+
52+
```js
53+
interface CreateSessionStoragePersistorOptions {
54+
/** The key to use when storing the cache to sessionstorage */
55+
sessionStorageKey?: string
56+
/** To avoid sessionstorage spamming,
57+
* pass a time in ms to throttle saving the cache to disk */
58+
throttleTime?: number
59+
}
60+
```
61+
62+
The default options are:
63+
64+
```js
65+
{
66+
sessionStorageKey = `REACT_QUERY_OFFLINE_CACHE`,
67+
throttleTime = 1000,
68+
}
69+
```

docs/src/pages/plugins/persistQueryClient.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ title: persistQueryClient (Experimental)
1010
## Officially Supported Persistors
1111

1212
- [createLocalStoragePersistor (Experimental)](/plugins/createLocalStoragePersistor)
13+
- [createSessionStoragePersistor (Experimental)](/plugins/createSessionStoragePersistor)
1314

1415
## Installation
1516

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"devtools",
5353
"persistQueryClient-experimental",
5454
"createLocalStoragePersistor-experimental",
55+
"createSessionStoragePersistor-experimental",
5556
"broadcastQueryClient-experimental",
5657
"lib",
5758
"react",

rollup.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ const inputSrcs = [
3030
'ReactQueryCreateLocalStoragePersistorExperimental',
3131
'createLocalStoragePersistor-experimental',
3232
],
33+
[
34+
'src/createSessionStoragePersistor-experimental/index.ts',
35+
'ReactQueryCreateSessionStoragePersistorExperimental',
36+
'createSessionStoragePersistor-experimental',
37+
],
3338
[
3439
'src/broadcastQueryClient-experimental/index.ts',
3540
'ReactQueryBroadcastQueryClientExperimental',
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { noop } from '../core/utils'
2+
import { PersistedClient, Persistor } from '../persistQueryClient-experimental'
3+
4+
interface CreateSessionStoragePersistorOptions {
5+
/** The key to use when storing the cache to sessionstorage */
6+
sessionStorageKey?: string
7+
/** To avoid sessionstorage spamming,
8+
* pass a time in ms to throttle saving the cache to disk */
9+
throttleTime?: number
10+
}
11+
12+
export function createSessionStoragePersistor({
13+
sessionStorageKey = `REACT_QUERY_OFFLINE_CACHE`,
14+
throttleTime = 1000,
15+
}: CreateSessionStoragePersistorOptions = {}): Persistor {
16+
if (typeof sessionStorage !== 'undefined') {
17+
return {
18+
persistClient: throttle(persistedClient => {
19+
sessionStorage.setItem(sessionStorageKey, JSON.stringify(persistedClient))
20+
}, throttleTime),
21+
restoreClient: () => {
22+
const cacheString = sessionStorage.getItem(sessionStorageKey)
23+
24+
if (!cacheString) {
25+
return
26+
}
27+
28+
return JSON.parse(cacheString) as PersistedClient
29+
},
30+
removeClient: () => {
31+
sessionStorage.removeItem(sessionStorageKey)
32+
},
33+
}
34+
}
35+
36+
return {
37+
persistClient: noop,
38+
restoreClient: noop,
39+
removeClient: noop,
40+
}
41+
}
42+
43+
function throttle<TArgs extends any[]>(
44+
func: (...args: TArgs) => any,
45+
wait = 100
46+
) {
47+
let timer: number | null = null
48+
49+
return function (...args: TArgs) {
50+
if (timer === null) {
51+
timer = setTimeout(() => {
52+
func(...args)
53+
timer = null
54+
}, wait)
55+
}
56+
}
57+
}

tsconfig.types.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"./src/devtools/index.ts",
1616
"./src/persistQueryClient-experimental/index.ts",
1717
"./src/createLocalStoragePersistor-experimental/index.ts",
18+
"./src/createSessionStoragePersistor-experimental/index.ts",
1819
"./src/broadcastQueryClient-experimental/index.ts"
1920
],
2021
"exclude": ["./src/**/*"]

0 commit comments

Comments
 (0)