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