Open History to see the last commit
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
18}
19β
20export interface HydrateOptions {
21 defaultOptions?: {
22 queries?: QueryOptions
23 mutations?: MutationOptions
24 }
25}
26β
27interface DehydratedMutation {
28 mutationKey?: MutationKey
29 state: MutationState
30}
31β
32interface DehydratedQuery {
33 queryHash: string
34 queryKey: QueryKey
35 state: QueryState
36}
37β
38export interface DehydratedState {
39 mutations: DehydratedMutation[]
40 queries: DehydratedQuery[]
41}
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 }
54}
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 }
66}
67β
68export function defaultShouldDehydrateMutation(mutation: Mutation) {
69 return mutation.state.isPaused
70}
71β
72export function defaultShouldDehydrateQuery(query: Query) {
73 return query.state.status === 'success'
74}
75β
76export function dehydrate(
77 client: QueryClient,
78 options: DehydrateOptions = {},
79): DehydratedState {
80 const mutations: DehydratedMutation[] = []
81 const queries: DehydratedQuery[] = []
82β
83 if (options.dehydrateMutations !== false) {
84 const shouldDehydrateMutation =
85 options.shouldDehydrateMutation || defaultShouldDehydrateMutation
86β
87 client
88 .getMutationCache()
89 .getAll()
90 .forEach((mutation) => {
91 if (shouldDehydrateMutation(mutation)) {
92 mutations.push(dehydrateMutation(mutation))
93 }
94 })
95 }
96β
97 if (options.dehydrateQueries !== false) {
98 const shouldDehydrateQuery =
99 options.shouldDehydrateQuery || defaultShouldDehydrateQuery
100β
101 client
102 .getQueryCache()
103 .getAll()
104 .forEach((query) => {
105 if (shouldDehydrateQuery(query)) {
106 queries.push(dehydrateQuery(query))
107 }
108 })
109 }
110β
111 return { mutations, queries }
112}
113β
114export function hydrate(
115 client: QueryClient,
116 dehydratedState: unknown,
117 options?: HydrateOptions,
118): void {
119 if (typeof dehydratedState !== 'object' || dehydratedState === null) {
120 return
121 }
122β
123 const mutationCache = client.getMutationCache()
124 const queryCache = client.getQueryCache()
125β
126 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
127 const mutations = (dehydratedState as DehydratedState).mutations || []
128 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
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(({ queryKey, state, queryHash }) => {
143 const query = queryCache.get(queryHash)
144β
145 // Do not hydrate if an existing query exists with newer data
146 if (query) {
147 if (query.state.dataUpdatedAt < state.dataUpdatedAt) {
148 // omit fetchStatus from dehydrated state
149 // so that query stays in its current fetchStatus
150 const { fetchStatus: _ignored, ...dehydratedQueryState } = state
151 query.setState(dehydratedQueryState)
152 }
153 return
154 }
155β
156 // Restore query
157 queryCache.build(
158 client,
159 {
160 ...options?.defaultOptions?.queries,
161 queryKey,
162 queryHash,
163 },
164 // Reset fetch status to idle to avoid
165 // query being stuck in fetching state upon hydration
166 {
167 ...state,
168 fetchStatus: 'idle',
169 },
170 )
171 })
172}