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

hydration.ts

164 lines4.1 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 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 }
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((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 })