Skip to content

Commit cbd5df0

Browse files
committed
fix(experimental_createQueryPersister): return more utilities, rename persister
1 parent 2121836 commit cbd5df0

File tree

7 files changed

+177
-81
lines changed

7 files changed

+177
-81
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.
@@ -48,16 +48,18 @@ Garbage collecting a Query from memory **does not** affect the persisted data. T
4848
```tsx
4949
import AsyncStorage from '@react-native-async-storage/async-storage'
5050
import { QueryClient } from '@tanstack/react-query'
51-
import { experimental_createPersister } from '@tanstack/query-persist-client-core'
51+
import { experimental_createQueryPersister } from '@tanstack/query-persist-client-core'
52+
53+
const persister = experimental_createQueryPersister({
54+
storage: AsyncStorage,
55+
maxAge: 1000 * 60 * 60 * 12, // 12 hours
56+
})
5257

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

7072
## API
7173

72-
### `experimental_createPersister`
74+
### `experimental_createQueryPersister`
7375

7476
```tsx
75-
experimental_createPersister(options: StoragePersisterOptions)
77+
experimental_createQueryPersister(options: StoragePersisterOptions)
7678
```
7779

7880
#### `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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'
22
import { Query, QueryClient, hashKey } from '@tanstack/query-core'
33
import {
44
PERSISTER_KEY_PREFIX,
5-
experimental_createPersister,
5+
experimental_createQueryPersister,
66
} from '../createPersister'
77
import type { QueryFunctionContext, QueryKey } from '@tanstack/query-core'
88
import type { StoragePersisterOptions } from '../createPersister'
@@ -39,7 +39,7 @@ function setupPersister(
3939

4040
const queryFn = vi.fn()
4141

42-
const persisterFn = experimental_createPersister(persisterOptions)
42+
const { persisterFn } = experimental_createQueryPersister(persisterOptions)
4343

4444
const query = new Query({
4545
client,
@@ -213,7 +213,7 @@ describe('createPersister', () => {
213213
storageKey,
214214
JSON.stringify({
215215
buster: '',
216-
state: { dataUpdatedAt },
216+
state: { dataUpdatedAt, data: '' },
217217
}),
218218
)
219219

@@ -242,7 +242,7 @@ describe('createPersister', () => {
242242
storageKey,
243243
JSON.stringify({
244244
buster: '',
245-
state: { dataUpdatedAt: Date.now() },
245+
state: { dataUpdatedAt: Date.now(), data: '' },
246246
}),
247247
)
248248

@@ -336,7 +336,7 @@ describe('createPersister', () => {
336336
storageKey,
337337
JSON.stringify({
338338
buster: '',
339-
state: { dataUpdatedAt: Date.now() },
339+
state: { dataUpdatedAt: Date.now(), data: '' },
340340
}),
341341
)
342342

0 commit comments

Comments
 (0)