Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/src/pages/plugins/persistQueryClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ interface PersistQueryClientOptions {
/** A unique string that can be used to forcefully
* invalidate existing caches if they do not share the same buster string */
buster?: string
/** The options passed to the hydrate function */
hydrateOptions?: HydrateOptions
/** The options passed to the dehydrate function */
dehydrateOptions?: DehydrateOptions
}
```

Expand Down
12 changes: 9 additions & 3 deletions src/persistQueryClient-experimental/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { QueryClient } from '../core'
import { getLogger } from '../core/logger'
import { dehydrate, DehydratedState, hydrate } from '../hydration'
import { dehydrate, DehydratedState, DehydrateOptions, HydrateOptions, hydrate } from '../hydration'
import { Promisable } from 'type-fest'

export interface Persistor {
Expand Down Expand Up @@ -28,21 +28,27 @@ export interface PersistQueryClientOptions {
/** A unique string that can be used to forcefully
* invalidate existing caches if they do not share the same buster string */
buster?: string
/** The options passed to the hydrate function */
hydrateOptions?: HydrateOptions
/** The options passed to the dehydrate function */
dehydrateOptions?: DehydrateOptions
}

export async function persistQueryClient({
queryClient,
persistor,
maxAge = 1000 * 60 * 60 * 24,
buster = '',
hydrateOptions,
dehydrateOptions,
}: PersistQueryClientOptions) {
if (typeof window !== 'undefined') {
// Subscribe to changes
const saveClient = () => {
const persistClient: PersistedClient = {
buster,
timestamp: Date.now(),
clientState: dehydrate(queryClient),
clientState: dehydrate(queryClient, dehydrateOptions),
}

persistor.persistClient(persistClient)
Expand All @@ -59,7 +65,7 @@ export async function persistQueryClient({
if (expired || busted) {
persistor.removeClient()
} else {
hydrate(queryClient, persistedClient.clientState)
hydrate(queryClient, persistedClient.clientState, hydrateOptions)
}
} else {
persistor.removeClient()
Expand Down