Skip to content

Commit

Permalink
feat: session storage persistor (TanStack#2282)
Browse files Browse the repository at this point in the history
LocalStoragePersistor is now integrated with WebStoragePersistor, which can take any `Storage`
  • Loading branch information
CurtisHughes authored Jun 11, 2021
1 parent 98dd91f commit be6bfbb
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 44 deletions.
6 changes: 0 additions & 6 deletions createLocalStoragePersistor-experimental/package.json

This file was deleted.

6 changes: 6 additions & 0 deletions createWebStoragePersistor-experimental/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"internal": true,
"main": "../lib/createWebStoragePersistor-experimental/index.js",
"module": "../es/createWebStoragePersistor-experimental/index.js",
"types": "../types/createWebStoragePersistor-experimental/index.d.ts"
}
6 changes: 3 additions & 3 deletions docs/src/manifests/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@
"editUrl": "/plugins/persistQueryClient.md"
},
{
"title": "createLocalStoragePersistor (Experimental)",
"path": "/plugins/createLocalStoragePersistor",
"editUrl": "/plugins/createLocalStoragePersistor.md"
"title": "createWebStoragePersistor (Experimental)",
"path": "/plugins/createWebStoragePersistor",
"editUrl": "/plugins/createWebStoragePersistor.md"
},
{
"title": "broadcastQueryClient (Experimental)",
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/guides/migrating-to-react-query-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Previous versions of React Query were awesome and brought some amazing new featu
- Observe queries/mutations outside of React
- Use the React Query core logic anywhere you want!
- Bundled/Colocated Devtools via `react-query/devtools`
- Cache Persistence to localstorage (experimental via `react-query/persistQueryClient-experimental` and `react-query/createLocalStoragePersistor-experimental`)
- Cache Persistence to web storage (experimental via `react-query/persistQueryClient-experimental` and `react-query/createWebStoragePersistor-experimental`)

## Breaking Changes

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
---
id: createLocalStoragePersistor
title: createLocalStoragePersistor (Experimental)
id: createWebStoragePersistor
title: createWebStoragePersistor (Experimental)
---

> 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.
## Installation

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

## Usage

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

```ts
import { persistQueryClient } from 'react-query/persistQueryClient-experimental'
import { createLocalStoragePersistor } from 'react-query/createLocalStoragePersistor-experimental'
import { createWebStoragePersistor } from 'react-query/createWebStoragePersistor-experimental'

const queryClient = new QueryClient({
defaultOptions: {
Expand All @@ -27,7 +27,8 @@ const queryClient = new QueryClient({
},
})

const localStoragePersistor = createLocalStoragePersistor()
const localStoragePersistor = createWebStoragePersistor({storage: window.localStorage})
// const sessionStoragePersistor = createWebStoragePersistor({storage: window.sessionStorage})

persistQueryClient({
queryClient,
Expand All @@ -37,23 +38,25 @@ persistQueryClient({

## API

### `createLocalStoragePersistor`
### `createWebStoragePersistor`

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

```js
createLocalStoragePersistor(options?: CreateLocalStoragePersistorOptions)
createWebStoragePersistor(options?: CreateWebStoragePersistorOptions)
```

### `Options`

An optional object of options:

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

```js
{
localStorageKey = `REACT_QUERY_OFFLINE_CACHE`,
key = `REACT_QUERY_OFFLINE_CACHE`,
throttleTime = 1000,
}
```
6 changes: 3 additions & 3 deletions docs/src/pages/plugins/persistQueryClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ title: persistQueryClient (Experimental)

## Officially Supported Persistors

- [createLocalStoragePersistor (Experimental)](/plugins/createLocalStoragePersistor)
- [createWebStoragePersistor (Experimental)](/plugins/createWebStoragePersistor)

## Installation

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

```ts
import { persistQueryClient } from 'react-query/persistQueryClient-experimental'
import { createLocalStoragePersistor } from 'react-query/createLocalStoragePersistor-experimental'
import { createWebStoragePersistor } from 'react-query/createWebStoragePersistor-experimental'

const queryClient = new QueryClient({
defaultOptions: {
Expand All @@ -31,7 +31,7 @@ const queryClient = new QueryClient({
},
})

const localStoragePersistor = createLocalStoragePersistor()
const localStoragePersistor = createWebStoragePersistor({storage: window.localStorage})

persistQueryClient({
queryClient,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"hydration",
"devtools",
"persistQueryClient-experimental",
"createLocalStoragePersistor-experimental",
"createWebStoragePersistor-experimental",
"broadcastQueryClient-experimental",
"lib",
"react",
Expand Down
6 changes: 3 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const inputSrcs = [
'persistQueryClient-experimental',
],
[
'src/createLocalStoragePersistor-experimental/index.ts',
'ReactQueryCreateLocalStoragePersistorExperimental',
'createLocalStoragePersistor-experimental',
'src/createWebStoragePersistor-experimental/index.ts',
'ReactQueryCreateWebStoragePersistorExperimental',
'createWebStoragePersistor-experimental',
],
[
'src/broadcastQueryClient-experimental/index.ts',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import { noop } from '../core/utils'
import { PersistedClient, Persistor } from '../persistQueryClient-experimental'

interface CreateLocalStoragePersistorOptions {
/** The key to use when storing the cache to localstorage */
localStorageKey?: string
/** To avoid localstorage spamming,
interface CreateWebStoragePersistorOptions {
/** The storage client used for setting an retrieving items from cache */
storage: Storage
/** The key to use when storing the cache */
key?: string
/** To avoid spamming,
* pass a time in ms to throttle saving the cache to disk */
throttleTime?: number
}

export function createLocalStoragePersistor({
localStorageKey = `REACT_QUERY_OFFLINE_CACHE`,
export function createWebStoragePersistor({
storage,
key = `REACT_QUERY_OFFLINE_CACHE`,
throttleTime = 1000,
}: CreateLocalStoragePersistorOptions = {}): Persistor {
if (typeof localStorage !== 'undefined') {
}: CreateWebStoragePersistorOptions): Persistor {
if (typeof storage !== 'undefined') {
return {
persistClient: throttle(persistedClient => {
localStorage.setItem(localStorageKey, JSON.stringify(persistedClient))
storage.setItem(key, JSON.stringify(persistedClient))
}, throttleTime),
restoreClient: () => {
const cacheString = localStorage.getItem(localStorageKey)
const cacheString = storage.getItem(key)

if (!cacheString) {
return
Expand All @@ -28,7 +31,7 @@ export function createLocalStoragePersistor({
return JSON.parse(cacheString) as PersistedClient
},
removeClient: () => {
localStorage.removeItem(localStorageKey)
storage.removeItem(key)
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.types.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"./src/hydration/index.ts",
"./src/devtools/index.ts",
"./src/persistQueryClient-experimental/index.ts",
"./src/createLocalStoragePersistor-experimental/index.ts",
"./src/createWebStoragePersistor-experimental/index.ts",
"./src/broadcastQueryClient-experimental/index.ts"
],
"exclude": ["./src/**/*"]
Expand Down

0 comments on commit be6bfbb

Please sign in to comment.