Skip to content

Commit dcb3e7f

Browse files
committed
feat: add support for default options when hydrating
1 parent 1de4ca9 commit dcb3e7f

File tree

8 files changed

+76
-26
lines changed

8 files changed

+76
-26
lines changed

docs/src/pages/reference/hydration/HydrateComp.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ function App() {
1717

1818
- `state: DehydratedState`
1919
- The state to hydrate
20+
- `options: HydrateOptions`
21+
- Optional
22+
- `defaultOptions: QueryOptions`
23+
- The default query options to use for the hydrated queries.

docs/src/pages/reference/hydration/dehydrate.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ const dehydratedState = dehydrate(cache, {
1818
- `cache: QueryCache`
1919
- **Required**
2020
- The `cache` that should be dehydrated
21-
- `shouldDehydrate: (query: Query) => boolean`
22-
- This function is called for each query in the cache
23-
- Return `true` to include this query in dehydration, or `false` otherwise
24-
- Default version only includes successful queries, do `shouldDehydrate: () => true` to include all queries
21+
- `options: DehydrateOptions`
22+
- Optional
23+
- `shouldDehydrate: (query: Query) => boolean`
24+
- This function is called for each query in the cache
25+
- Return `true` to include this query in dehydration, or `false` otherwise
26+
- Default version only includes successful queries, do `shouldDehydrate: () => true` to include all queries
2527

2628
**Returns**
2729

docs/src/pages/reference/hydration/hydrate.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ title: hydration/hydrate
88
```js
99
import { hydrate } from 'react-query/hydration'
1010

11-
hydrate(cache, dehydratedState)
11+
hydrate(cache, dehydratedState, options)
1212
```
1313

1414
**Options**
@@ -19,3 +19,7 @@ hydrate(cache, dehydratedState)
1919
- `dehydratedState: DehydratedState`
2020
- **Required**
2121
- The state to hydrate into the cache
22+
- `options: HydrateOptions`
23+
- Optional
24+
- `defaultOptions: QueryOptions`
25+
- The default query options to use for the hydrated queries.

docs/src/pages/reference/hydration/useHydrate.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ title: hydration/useHydrate
88
```jsx
99
import { useHydrate } from 'react-query/hydration'
1010

11-
useHydrate(dehydratedState)
11+
useHydrate(dehydratedState, options)
1212
```
1313

1414
**Options**
1515

1616
- `dehydratedState: DehydratedState`
1717
- **Required**
1818
- The state to hydrate
19+
- `options: HydrateOptions`
20+
- Optional
21+
- `defaultOptions: QueryOptions`
22+
- The default query options to use for the hydrated queries.

src/hydration/hydration.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import { Query } from '../core/query'
22
import { QueryCache } from '../core/queryCache'
3-
import { QueryKey } from '../core/types'
3+
import { QueryKey, QueryOptions } from '../core/types'
44

5-
export interface DehydratedQueryConfig {
5+
// TYPES
6+
7+
export interface DehydrateOptions {
8+
shouldDehydrate?: ShouldDehydrateFunction
9+
}
10+
11+
export interface HydrateOptions {
12+
defaultOptions?: QueryOptions
13+
}
14+
15+
interface DehydratedQueryConfig {
616
cacheTime: number
717
}
818

9-
export interface DehydratedQuery {
19+
interface DehydratedQuery {
1020
queryKey: QueryKey
1121
queryHash: string
1222
data?: unknown
@@ -20,9 +30,7 @@ export interface DehydratedState {
2030

2131
export type ShouldDehydrateFunction = (query: Query) => boolean
2232

23-
export interface DehydrateConfig {
24-
shouldDehydrate?: ShouldDehydrateFunction
25-
}
33+
// FUNCTIONS
2634

2735
function serializePositiveNumber(value: number): number {
2836
return value === Infinity ? -1 : value
@@ -54,10 +62,11 @@ function defaultShouldDehydrate(query: Query) {
5462

5563
export function dehydrate(
5664
cache: QueryCache,
57-
dehydrateConfig?: DehydrateConfig
65+
options?: DehydrateOptions
5866
): DehydratedState {
59-
const config = dehydrateConfig || {}
60-
const shouldDehydrate = config.shouldDehydrate || defaultShouldDehydrate
67+
options = options || {}
68+
69+
const shouldDehydrate = options.shouldDehydrate || defaultShouldDehydrate
6170
const queries: DehydratedQuery[] = []
6271

6372
cache.getAll().forEach(query => {
@@ -69,11 +78,16 @@ export function dehydrate(
6978
return { queries }
7079
}
7180

72-
export function hydrate(cache: QueryCache, dehydratedState: unknown): void {
81+
export function hydrate(
82+
cache: QueryCache,
83+
dehydratedState: unknown,
84+
options?: HydrateOptions
85+
): void {
7386
if (typeof dehydratedState !== 'object' || dehydratedState === null) {
7487
return
7588
}
7689

90+
const defaultOptions = options?.defaultOptions || {}
7791
const queries = (dehydratedState as DehydratedState).queries || []
7892

7993
queries.forEach(dehydratedQuery => {
@@ -90,6 +104,7 @@ export function hydrate(cache: QueryCache, dehydratedState: unknown): void {
90104
queryKey: dehydratedQuery.queryKey,
91105
queryHash: dehydratedQuery.queryHash,
92106
options: {
107+
...defaultOptions,
93108
cacheTime: deserializePositiveNumber(
94109
dehydratedQuery.config.cacheTime
95110
),

src/hydration/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ export { useHydrate, Hydrate } from './react'
33

44
// Types
55
export type {
6-
DehydratedQueryConfig,
7-
DehydratedQuery,
6+
DehydrateOptions,
87
DehydratedState,
8+
HydrateOptions,
99
ShouldDehydrateFunction,
10-
DehydrateConfig,
1110
} from './hydration'
1211
export type { HydrateProps } from './react'

src/hydration/react.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
11
import React from 'react'
22

33
import { useQueryClient } from '../react'
4-
import { hydrate } from './hydration'
4+
import { hydrate, HydrateOptions } from './hydration'
55

6-
export function useHydrate(queries: unknown) {
6+
export function useHydrate(state: unknown, options?: HydrateOptions) {
77
const client = useQueryClient()
88
const cache = client.getCache()
99

10+
const optionsRef = React.useRef(options)
11+
optionsRef.current = options
12+
1013
// Running hydrate again with the same queries is safe,
1114
// it wont overwrite or initialize existing queries,
1215
// relying on useMemo here is only a performance optimization
1316
React.useMemo(() => {
14-
if (queries) {
15-
hydrate(cache, queries)
17+
if (state) {
18+
hydrate(cache, state, optionsRef.current)
1619
}
17-
}, [cache, queries])
20+
}, [cache, state])
1821
}
1922

2023
export interface HydrateProps {
2124
state?: unknown
25+
options?: HydrateOptions
2226
}
2327

24-
export const Hydrate: React.FC<HydrateProps> = ({ state, children }) => {
25-
useHydrate(state)
28+
export const Hydrate: React.FC<HydrateProps> = ({
29+
children,
30+
options,
31+
state,
32+
}) => {
33+
useHydrate(state, options)
2634
return children as React.ReactElement<any>
2735
}

src/hydration/tests/hydration.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ describe('dehydration and rehydration', () => {
104104
hydrationCache.clear()
105105
})
106106

107+
test('should be able to provide default options for the hydrated queries', async () => {
108+
const cache = new QueryCache()
109+
const client = new QueryClient({ cache })
110+
await client.prefetchQuery('string', () => fetchData('string'))
111+
const dehydrated = dehydrate(cache)
112+
const stringified = JSON.stringify(dehydrated)
113+
const parsed = JSON.parse(stringified)
114+
const hydrationCache = new QueryCache()
115+
hydrate(hydrationCache, parsed, { defaultOptions: { retry: 10 } })
116+
expect(hydrationCache.find('string')?.options.retry).toBe(10)
117+
cache.clear()
118+
hydrationCache.clear()
119+
})
120+
107121
test('should work with complex keys', async () => {
108122
const cache = new QueryCache()
109123
const client = new QueryClient({ cache })

0 commit comments

Comments
 (0)