Skip to content

Commit 9626c1b

Browse files
authored
feat(persist): Added hydrate and dehydrate options to the persistQueryClient function (TanStack#2131)
1 parent 79ad5a9 commit 9626c1b

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

docs/src/pages/plugins/persistQueryClient.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ interface PersistQueryClientOptions {
9494
/** A unique string that can be used to forcefully
9595
* invalidate existing caches if they do not share the same buster string */
9696
buster?: string
97+
/** The options passed to the hydrate function */
98+
hydrateOptions?: HydrateOptions
99+
/** The options passed to the dehydrate function */
100+
dehydrateOptions?: DehydrateOptions
97101
}
98102
```
99103

src/persistQueryClient-experimental/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { QueryClient } from '../core'
22
import { getLogger } from '../core/logger'
3-
import { dehydrate, DehydratedState, hydrate } from '../hydration'
3+
import { dehydrate, DehydratedState, DehydrateOptions, HydrateOptions, hydrate } from '../hydration'
44
import { Promisable } from 'type-fest'
55

66
export interface Persistor {
@@ -28,21 +28,27 @@ export interface PersistQueryClientOptions {
2828
/** A unique string that can be used to forcefully
2929
* invalidate existing caches if they do not share the same buster string */
3030
buster?: string
31+
/** The options passed to the hydrate function */
32+
hydrateOptions?: HydrateOptions
33+
/** The options passed to the dehydrate function */
34+
dehydrateOptions?: DehydrateOptions
3135
}
3236

3337
export async function persistQueryClient({
3438
queryClient,
3539
persistor,
3640
maxAge = 1000 * 60 * 60 * 24,
3741
buster = '',
42+
hydrateOptions,
43+
dehydrateOptions,
3844
}: PersistQueryClientOptions) {
3945
if (typeof window !== 'undefined') {
4046
// Subscribe to changes
4147
const saveClient = () => {
4248
const persistClient: PersistedClient = {
4349
buster,
4450
timestamp: Date.now(),
45-
clientState: dehydrate(queryClient),
51+
clientState: dehydrate(queryClient, dehydrateOptions),
4652
}
4753

4854
persistor.persistClient(persistClient)
@@ -59,7 +65,7 @@ export async function persistQueryClient({
5965
if (expired || busted) {
6066
persistor.removeClient()
6167
} else {
62-
hydrate(queryClient, persistedClient.clientState)
68+
hydrate(queryClient, persistedClient.clientState, hydrateOptions)
6369
}
6470
} else {
6571
persistor.removeClient()

0 commit comments

Comments
 (0)