|  | 
|  | 1 | +--- | 
|  | 2 | +id: createPersister | 
|  | 3 | +title: experimental_createPersister | 
|  | 4 | +--- | 
|  | 5 | + | 
|  | 6 | +## Installation | 
|  | 7 | + | 
|  | 8 | +This utility comes as a separate package and is available under the `'@tanstack/query-persist-client-core'` import. | 
|  | 9 | + | 
|  | 10 | +```bash | 
|  | 11 | +npm install @tanstack/query-persist-client-core | 
|  | 12 | +``` | 
|  | 13 | + | 
|  | 14 | +or | 
|  | 15 | + | 
|  | 16 | +```bash | 
|  | 17 | +pnpm add @tanstack/query-persist-client-core | 
|  | 18 | +``` | 
|  | 19 | + | 
|  | 20 | +or | 
|  | 21 | + | 
|  | 22 | +```bash | 
|  | 23 | +yarn add @tanstack/query-persist-client-core | 
|  | 24 | +``` | 
|  | 25 | + | 
|  | 26 | +> Note: This util is also included in the `@tanstack/react-query-persist-client` package, so you do not need to install it separately if you are using that package. | 
|  | 27 | +
 | 
|  | 28 | +## Usage | 
|  | 29 | + | 
|  | 30 | +- Import the `experimental_createPersister` function | 
|  | 31 | +- Create a new `experimental_createPersister` | 
|  | 32 | +  - you can pass any `storage` to it that adheres to the `AsyncStorage` or `Storage` interface - the example below uses the async-storage from React Native. | 
|  | 33 | +- 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. | 
|  | 34 | +  - 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. | 
|  | 35 | +  - If you provide this `persister` to a single `useQuery` hook, only this Query will be persisted. | 
|  | 36 | + | 
|  | 37 | +This 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. | 
|  | 38 | + | 
|  | 39 | +```tsx | 
|  | 40 | +import AsyncStorage from '@react-native-async-storage/async-storage' | 
|  | 41 | +import { QueryClient } from '@tanstack/react-query' | 
|  | 42 | +import { experimental_createPersister } from '@tanstack/query-persist-client-core' | 
|  | 43 | + | 
|  | 44 | +const queryClient = new QueryClient({ | 
|  | 45 | +  defaultOptions: { | 
|  | 46 | +    queries: { | 
|  | 47 | +      gcTime: 1000 * 60 * 60 * 24, // 24 hours | 
|  | 48 | +      persister: experimental_createPersister({ | 
|  | 49 | +        storage: AsyncStorage, | 
|  | 50 | +      }), | 
|  | 51 | +    }, | 
|  | 52 | +  }, | 
|  | 53 | +}) | 
|  | 54 | +``` | 
|  | 55 | + | 
|  | 56 | +## API | 
|  | 57 | + | 
|  | 58 | +### `experimental_createPersister` | 
|  | 59 | + | 
|  | 60 | +```tsx | 
|  | 61 | +experimental_createPersister(options: StoragePersisterOptions) | 
|  | 62 | +``` | 
|  | 63 | + | 
|  | 64 | +#### `Options` | 
|  | 65 | + | 
|  | 66 | +```tsx | 
|  | 67 | +export interface StoragePersisterOptions { | 
|  | 68 | +  /** The storage client used for setting and retrieving items from cache. | 
|  | 69 | +   * For SSR pass in `undefined`. | 
|  | 70 | +   */ | 
|  | 71 | +  storage: AsyncStorage | Storage | undefined | null | 
|  | 72 | +  /** | 
|  | 73 | +   * How to serialize the data to storage. | 
|  | 74 | +   * @default `JSON.stringify` | 
|  | 75 | +   */ | 
|  | 76 | +  serialize?: (persistedQuery: PersistedQuery) => string | 
|  | 77 | +  /** | 
|  | 78 | +   * How to deserialize the data from storage. | 
|  | 79 | +   * @default `JSON.parse` | 
|  | 80 | +   */ | 
|  | 81 | +  deserialize?: (cachedString: string) => PersistedQuery | 
|  | 82 | +  /** | 
|  | 83 | +   * A unique string that can be used to forcefully invalidate existing caches, | 
|  | 84 | +   * if they do not share the same buster string | 
|  | 85 | +   */ | 
|  | 86 | +  buster?: string | 
|  | 87 | +  /** | 
|  | 88 | +   * The max-allowed age of the cache in milliseconds. | 
|  | 89 | +   * If a persisted cache is found that is older than this | 
|  | 90 | +   * time, it will be discarded | 
|  | 91 | +   */ | 
|  | 92 | +  maxAge?: number | 
|  | 93 | +  /** | 
|  | 94 | +   * Prefix to be used for storage key. | 
|  | 95 | +   * Storage key is a combination of prefix and query hash in a form of `prefix-queryHash`. | 
|  | 96 | +   */ | 
|  | 97 | +  prefix?: string | 
|  | 98 | +  /** | 
|  | 99 | +   * Filters to narrow down which Queries should be persisted. | 
|  | 100 | +   */ | 
|  | 101 | +  filters?: QueryFilters | 
|  | 102 | +} | 
|  | 103 | + | 
|  | 104 | +interface AsyncStorage { | 
|  | 105 | +  getItem: (key: string) => Promise<string | undefined | null> | 
|  | 106 | +  setItem: (key: string, value: string) => Promise<unknown> | 
|  | 107 | +  removeItem: (key: string) => Promise<void> | 
|  | 108 | +} | 
|  | 109 | +``` | 
|  | 110 | + | 
|  | 111 | +The default options are: | 
|  | 112 | + | 
|  | 113 | +```tsx | 
|  | 114 | +{ | 
|  | 115 | +  prefix = 'tanstack-query', | 
|  | 116 | +  maxAge = 1000 * 60 * 60 * 24, | 
|  | 117 | +  serialize = JSON.stringify, | 
|  | 118 | +  deserialize = JSON.parse, | 
|  | 119 | +} | 
|  | 120 | +``` | 
0 commit comments