dehydrate creates a frozen representation of a cache that can later be hydrated with hydrate. This is useful for passing prefetched queries from server to client or persisting queries to localStorage or other persistent locations. It only includes currently successful queries by default.
import { dehydrate } from '@tanstack/vue-query'
const dehydratedState = dehydrate(queryClient, {
shouldDehydrateQuery,
shouldDehydrateMutation,
})Options
client: QueryClient
options: DehydrateOptions
Optional
shouldDehydrateMutation: (mutation: Mutation) => boolean
shouldDehydrateQuery: (query: Query) => boolean
serializeData?: (data: any) => any A function to transform (serialize) data during dehydration.
shouldRedactErrors?: (error: unknown) => boolean
Optional
Only applies to queries that are still pending at dehydration time — their promise is dehydrated too, and this function decides whether to redact the error if that promise later rejects.
The function is called with that rejection error
Defaults to redacting all such errors
Does not apply to query.state.error on already-settled queries — that error is included in the dehydrated state as-is, so sanitize it yourself via shouldDehydrateQuery if it may contain sensitive data
Returns
dehydratedState: DehydratedState
Some storage systems (such as browser Web Storage API) require values to be JSON serializable. If you need to dehydrate values that are not automatically serializable to JSON (like Error or undefined), you have to serialize them for yourself. Since only successful queries are included per default, to also include Errors, you have to provide shouldDehydrateQuery, e.g.:
// server
const state = dehydrate(client, { shouldDehydrateQuery: () => true }) // to also include Errors
const serializedState = mySerialize(state) // transform Error instances to objects
// client
const state = myDeserialize(serializedState) // transform objects back to Error instances
hydrate(client, state)hydrate adds a previously dehydrated state into a cache.
import { hydrate } from '@tanstack/vue-query'
hydrate(queryClient, dehydratedState, options)Options
client: QueryClient
dehydratedState: DehydratedState
options: HydrateOptions
If the queries you're trying to hydrate already exist in the queryCache, hydrate will only overwrite them if the data is newer than the data present in the cache. Otherwise, it will not get applied.