Skip to content

Commit 33ff831

Browse files
committed
fix(experimental_createQueryPersister): return more utilities, rename persister
1 parent d3073c8 commit 33ff831

File tree

7 files changed

+185
-89
lines changed

7 files changed

+185
-89
lines changed

docs/framework/react/plugins/createPersister.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: createPersister
3-
title: experimental_createPersister
3+
title: experimental_createQueryPersister
44
---
55

66
## Installation
@@ -33,8 +33,8 @@ bun add @tanstack/query-persist-client-core
3333
3434
## Usage
3535

36-
- Import the `experimental_createPersister` function
37-
- Create a new `experimental_createPersister`
36+
- Import the `experimental_createQueryPersister` function
37+
- Create a new `experimental_createQueryPersister`
3838
- 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.
3939
- 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.
4040
- 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.
@@ -47,16 +47,18 @@ Garbage collecting a Query from memory **does not** affect the persisted data. T
4747
```tsx
4848
import AsyncStorage from '@react-native-async-storage/async-storage'
4949
import { QueryClient } from '@tanstack/react-query'
50-
import { experimental_createPersister } from '@tanstack/query-persist-client-core'
50+
import { experimental_createQueryPersister } from '@tanstack/query-persist-client-core'
51+
52+
const persister = experimental_createQueryPersister({
53+
storage: AsyncStorage,
54+
maxAge: 1000 * 60 * 60 * 12, // 12 hours
55+
})
5156

5257
const queryClient = new QueryClient({
5358
defaultOptions: {
5459
queries: {
5560
gcTime: 1000 * 30, // 30 seconds
56-
persister: experimental_createPersister({
57-
storage: AsyncStorage,
58-
maxAge: 1000 * 60 * 60 * 12, // 12 hours
59-
}),
61+
persister: persister.persisterFn,
6062
},
6163
},
6264
})
@@ -68,10 +70,10 @@ The `createPersister` plugin technically wraps the `queryFn`, so it doesn't rest
6870

6971
## API
7072

71-
### `experimental_createPersister`
73+
### `experimental_createQueryPersister`
7274

7375
```tsx
74-
experimental_createPersister(options: StoragePersisterOptions)
76+
experimental_createQueryPersister(options: StoragePersisterOptions)
7577
```
7678

7779
#### `Options`

docs/framework/vue/plugins/createPersister.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: createPersister
3-
title: experimental_createPersister
3+
title: experimental_createQueryPersister
44
---
55

66
## Installation
@@ -31,8 +31,8 @@ bun add @tanstack/query-persist-client-core
3131

3232
## Usage
3333

34-
- Import the `experimental_createPersister` function
35-
- Create a new `experimental_createPersister`
34+
- Import the `experimental_createQueryPersister` function
35+
- Create a new `experimental_createQueryPersister`
3636
- you can pass any `storage` to it that adheres to the `AsyncStorage` or `Storage` interface
3737
- 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.
3838
- 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.
@@ -44,16 +44,18 @@ Garbage collecting a Query from memory **does not** affect the persisted data. T
4444

4545
```tsx
4646
import { QueryClient } from '@tanstack/vue-query'
47-
import { experimental_createPersister } from '@tanstack/query-persist-client-core'
47+
import { experimental_createQueryPersister } from '@tanstack/query-persist-client-core'
48+
49+
const persister = experimental_createQueryPersister({
50+
storage: AsyncStorage,
51+
maxAge: 1000 * 60 * 60 * 12, // 12 hours
52+
})
4853

4954
const queryClient = new QueryClient({
5055
defaultOptions: {
5156
queries: {
5257
gcTime: 1000 * 30, // 30 seconds
53-
persister: experimental_createPersister({
54-
storage: localStorage,
55-
maxAge: 1000 * 60 * 60 * 12, // 12 hours
56-
}),
58+
persister: persister.persisterFn,
5759
},
5860
},
5961
})
@@ -65,10 +67,10 @@ The `createPersister` plugin technically wraps the `queryFn`, so it doesn't rest
6567

6668
## API
6769

68-
### `experimental_createPersister`
70+
### `experimental_createQueryPersister`
6971

7072
```tsx
71-
experimental_createPersister(options: StoragePersisterOptions)
73+
experimental_createQueryPersister(options: StoragePersisterOptions)
7274
```
7375

7476
#### `Options`

examples/vue/persister/src/Post.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { defineComponent } from 'vue'
44
import { useQuery } from '@tanstack/vue-query'
55
66
import { Post } from './types'
7-
import { experimental_createPersister } from '@tanstack/query-persist-client-core'
7+
import { experimental_createQueryPersister } from '@tanstack/query-persist-client-core'
88
99
const fetcher = async (id: number): Promise<Post> =>
1010
await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`).then(
@@ -24,13 +24,13 @@ export default defineComponent({
2424
const { isPending, isError, isFetching, data, error } = useQuery({
2525
queryKey: ['post', props.postId] as const,
2626
queryFn: () => fetcher(props.postId),
27-
persister: experimental_createPersister({
27+
persister: experimental_createQueryPersister({
2828
storage: {
2929
getItem: (key: string) => get(key),
3030
setItem: (key: string, value: string) => set(key, value),
3131
removeItem: (key: string) => del(key),
3232
},
33-
}),
33+
}).persisterFn,
3434
})
3535
3636
return { isPending, isError, isFetching, data, error }

examples/vue/persister/src/Posts.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
import { defineComponent } from 'vue'
33
import { useQuery } from '@tanstack/vue-query'
4-
import { experimental_createPersister } from '@tanstack/query-persist-client-core'
4+
import { experimental_createQueryPersister } from '@tanstack/query-persist-client-core'
55
66
import { Post } from './types'
77
@@ -34,9 +34,9 @@ export default defineComponent({
3434
} = useQuery({
3535
queryKey: ['posts'] as const,
3636
queryFn: () => fetcher(),
37-
persister: experimental_createPersister({
37+
persister: experimental_createQueryPersister({
3838
storage: localStorage,
39-
}),
39+
}).persisterFn,
4040
staleTime: 5000,
4141
})
4242

packages/query-persist-client-core/src/__tests__/createPersister.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, expect, test, vi } from 'vitest'
22
import { Query, QueryCache, hashKey } from '@tanstack/query-core'
33
import {
44
PERSISTER_KEY_PREFIX,
5-
experimental_createPersister,
5+
experimental_createQueryPersister,
66
} from '../createPersister'
77
import { sleep } from './utils'
88
import type { StoragePersisterOptions } from '../createPersister'
@@ -12,10 +12,10 @@ function getFreshStorage() {
1212
const storage = new Map()
1313
return {
1414
getItem: (key: string) => Promise.resolve(storage.get(key)),
15-
setItem: async (key: string, value: unknown) => {
15+
setItem: (key: string, value: unknown) => {
1616
storage.set(key, value)
1717
},
18-
removeItem: async (key: string) => {
18+
removeItem: (key: string) => {
1919
storage.delete(key)
2020
},
2121
}
@@ -36,7 +36,7 @@ function setupPersister(
3636

3737
const queryFn = vi.fn()
3838

39-
const persisterFn = experimental_createPersister(persisterOptions)
39+
const { persisterFn } = experimental_createQueryPersister(persisterOptions)
4040

4141
const query = new Query({
4242
cache: new QueryCache(),
@@ -202,7 +202,7 @@ describe('createPersister', () => {
202202
storageKey,
203203
JSON.stringify({
204204
buster: '',
205-
state: { dataUpdatedAt },
205+
state: { dataUpdatedAt, data: '' },
206206
}),
207207
)
208208

@@ -231,7 +231,7 @@ describe('createPersister', () => {
231231
storageKey,
232232
JSON.stringify({
233233
buster: '',
234-
state: { dataUpdatedAt: Date.now() },
234+
state: { dataUpdatedAt: Date.now(), data: '' },
235235
}),
236236
)
237237

@@ -325,7 +325,7 @@ describe('createPersister', () => {
325325
storageKey,
326326
JSON.stringify({
327327
buster: '',
328-
state: { dataUpdatedAt: Date.now() },
328+
state: { dataUpdatedAt: Date.now(), data: '' },
329329
}),
330330
)
331331

0 commit comments

Comments
 (0)