Open History to see the last commit
1---
2id: createPersister
3title: experimental_createPersister
4---
5β
6## Installation
7β
8This utility comes as a separate package and is available under the `'@tanstack/query-persist-client-core'` import.
9β
10```bash
11npm install @tanstack/query-persist-client-core
12```
13β
14or
15β
16```bash
17pnpm add @tanstack/query-persist-client-core
18```
19β
20or
21β
22```bash
23yarn add @tanstack/query-persist-client-core
24```
25β
26or
27β
28```bash
29bun add @tanstack/query-persist-client-core
30```
31β
32## Usage
33β
34- Import the `experimental_createPersister` function
35- Create a new `experimental_createPersister`
36 - you can pass any `storage` to it that adheres to the `AsyncStorage` or `Storage` interface
37- Pass that `persister` as an option to your Query. This can be done either by passing it to the `defaultOptions` of the `QueryClient` or to any `useQuery` hook instance.
38 - If you pass this `persister` as `defaultOptions`, all queries will be persisted to the provided `storage`. You can additionally narrow this down by passing `filters`. In contrast to the `persistClient` plugin, this will not persist the whole query client as a single item, but each query separately. As a key, the query hash is used.
39 - If you provide this `persister` to a single `useQuery` hook, only this Query will be persisted.
40β
41This way, you do not need to store whole `QueryClient`, but choose what is worth to be persisted in your application. Each query is lazily restored (when the Query is first used) and persisted (after each run of the `queryFn`), so it does not need to be throttled. `staleTime` is also respected after restoring the Query, so if data is considered `stale`, it will be refetched immediately after restoring. If data is `fresh`, the `queryFn` will not run.
42β
43Garbage collecting a Query from memory **does not** affect the persisted data. That means Queries can be kept in memory for a shorter period of time to be more **memory efficient**. If they are used the next time, they will just be restored from the persistent storage again.
44β
45```tsx
46import { QueryClient } from '@tanstack/vue-query'
47import { experimental_createPersister } from '@tanstack/query-persist-client-core'
48β
49const queryClient = new QueryClient({
50 defaultOptions: {
51 queries: {
52 gcTime: 1000 * 30, // 30 seconds
53 persister: experimental_createPersister({
54 storage: localStorage,
55 maxAge: 1000 * 60 * 60 * 12, // 12 hours
56 }),
57 },
58 },
59})
60```
61β
62### Adapted defaults
63β
64The `createPersister` plugin technically wraps the `queryFn`, so it doesn't restore if the `queryFn` doesn't run. In that way, it acts as a caching layer between the Query and the network. Thus, the `networkMode` defaults to `'offlineFirst'` when a persister is used, so that restoring from the persistent storage can also happen even if there is no network connection.
65β
66## API
67β
68### `experimental_createPersister`
69β
70```tsx
71experimental_createPersister(options: StoragePersisterOptions)
72```
73β
74#### `Options`
75β
76```tsx
77export interface StoragePersisterOptions {
78 /** The storage client used for setting and retrieving items from cache.
79 * For SSR pass in `undefined`.
80 */
81 storage: AsyncStorage | Storage | undefined | null
82 /**
83 * How to serialize the data to storage.
84 * @default `JSON.stringify`
85 */
86 serialize?: (persistedQuery: PersistedQuery) => string
87 /**
88 * How to deserialize the data from storage.
89 * @default `JSON.parse`
90 */
91 deserialize?: (cachedString: string) => PersistedQuery
92 /**
93 * A unique string that can be used to forcefully invalidate existing caches,
94 * if they do not share the same buster string
95 */
96 buster?: string
97 /**
98 * The max-allowed age of the cache in milliseconds.
99 * If a persisted cache is found that is older than this
100 * time, it will be discarded
101 * @default 24 hours
102 */
103 maxAge?: number
104 /**
105 * Prefix to be used for storage key.
106 * Storage key is a combination of prefix and query hash in a form of `prefix-queryHash`.
107 */
108 prefix?: string
109 /**
110 * Filters to narrow down which Queries should be persisted.
111 */
112 filters?: QueryFilters
113}
114β
115interface AsyncStorage {
116 getItem: (key: string) => Promise<string | undefined | null>
117 setItem: (key: string, value: string) => Promise<unknown>
118 removeItem: (key: string) => Promise<void>
119}
120```
121β
122The default options are:
123β
124```tsx
125{
126 prefix = 'tanstack-query',
127 maxAge = 1000 * 60 * 60 * 24,
128 serialize = JSON.stringify,
129 deserialize = JSON.parse,
130}
131```