Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

hydration.ts

164 lines4.0 KB

1import type { QueryClient } from './queryClient'
2import type { Query, QueryState } from './query'
3import type {
4 MutationKey,
5 MutationOptions,
6 QueryKey,
7 QueryOptions,
8} from './types'
9import type { Mutation, MutationState } from './mutation'
10​
11// TYPES
12​
13export interface DehydrateOptions {
14 dehydrateMutations?: boolean
15 dehydrateQueries?: boolean
16 shouldDehydrateMutation?: ShouldDehydrateMutationFunction
17 shouldDehydrateQuery?: ShouldDehydrateQueryFunction
19​
20export interface HydrateOptions {
21 defaultOptions?: {
22 queries?: QueryOptions
23 mutations?: MutationOptions
24 }
26​
27interface DehydratedMutation {
28 mutationKey?: MutationKey
29 state: MutationState
31​
32interface DehydratedQuery {
33 queryHash: string
34 queryKey: QueryKey
35 state: QueryState
37​
38export interface DehydratedState {
39 mutations: DehydratedMutation[]
40 queries: DehydratedQuery[]
42​
43export type ShouldDehydrateQueryFunction = (query: Query) => boolean
44​
45export type ShouldDehydrateMutationFunction = (mutation: Mutation) => boolean
46​
47// FUNCTIONS
48​
49function dehydrateMutation(mutation: Mutation): DehydratedMutation {
50 return {
51 mutationKey: mutation.options.mutationKey,
52 state: mutation.state,
53 }
55​
56// Most config is not dehydrated but instead meant to configure again when
57// consuming the de/rehydrated data, typically with useQuery on the client.
58// Sometimes it might make sense to prefetch data on the server and include
59// in the html-payload, but not consume it on the initial render.
60function dehydrateQuery(query: Query): DehydratedQuery {
61 return {
62 state: query.state,
63 queryKey: query.queryKey,
64 queryHash: query.queryHash,
65 }
67​
68function defaultShouldDehydrateMutation(mutation: Mutation) {
69 return mutation.state.isPaused
71​
72function defaultShouldDehydrateQuery(query: Query) {
73 return query.state.status === 'success'
75​
76export function dehydrate(
77 client: QueryClient,
78 options?: DehydrateOptions
79): DehydratedState {
80 options = options || {}
81​
82 const mutations: DehydratedMutation[] = []
83 const queries: DehydratedQuery[] = []
84​
85 if (options?.dehydrateMutations !== false) {
86 const shouldDehydrateMutation =
87 options.shouldDehydrateMutation || defaultShouldDehydrateMutation
88​
89 client
90 .getMutationCache()
91 .getAll()
92 .forEach(mutation => {
93 if (shouldDehydrateMutation(mutation)) {
94 mutations.push(dehydrateMutation(mutation))
95 }
96 })
97 }
98​
99 if (options?.dehydrateQueries !== false) {
100 const shouldDehydrateQuery =
101 options.shouldDehydrateQuery || defaultShouldDehydrateQuery
102​
103 client
104 .getQueryCache()
105 .getAll()
106 .forEach(query => {
107 if (shouldDehydrateQuery(query)) {
108 queries.push(dehydrateQuery(query))
109 }
110 })
111 }
112​
113 return { mutations, queries }
115​
116export function hydrate(
117 client: QueryClient,
118 dehydratedState: unknown,
119 options?: HydrateOptions
120): void {
121 if (typeof dehydratedState !== 'object' || dehydratedState === null) {
122 return
123 }
124​
125 const mutationCache = client.getMutationCache()
126 const queryCache = client.getQueryCache()
127​
128 const mutations = (dehydratedState as DehydratedState).mutations || []
129 const queries = (dehydratedState as DehydratedState).queries || []
130​
131 mutations.forEach(dehydratedMutation => {
132 mutationCache.build(
133 client,
134 {
135 ...options?.defaultOptions?.mutations,
136 mutationKey: dehydratedMutation.mutationKey,
137 },
138 dehydratedMutation.state
139 )
140 })
141​
142 queries.forEach(dehydratedQuery => {
143 const query = queryCache.get(dehydratedQuery.queryHash)
144​
145 // Do not hydrate if an existing query exists with newer data
146 if (query) {
147 if (query.state.dataUpdatedAt < dehydratedQuery.state.dataUpdatedAt) {
148 query.setState(dehydratedQuery.state)
149 }
150 return
151 }
152​
153 // Restore query
154 queryCache.build(
155 client,
156 {
157 ...options?.defaultOptions?.queries,
158 queryKey: dehydratedQuery.queryKey,
159 queryHash: dehydratedQuery.queryHash,
160 },
161 dehydratedQuery.state
162 )
163 })