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