@@ -29,9 +29,6 @@ export const HydrationBoundary = ({
29
29
queryClient,
30
30
} : HydrationBoundaryProps ) => {
31
31
const client = useQueryClient ( queryClient )
32
- const [ hydrationQueue , setHydrationQueue ] = React . useState <
33
- DehydratedState [ 'queries' ] | undefined
34
- > ( )
35
32
36
33
const optionsRef = React . useRef ( options )
37
34
optionsRef . current = options
@@ -51,66 +48,59 @@ export const HydrationBoundary = ({
51
48
// If the transition is aborted, we will have hydrated any _new_ queries, but
52
49
// we throw away the fresh data for any existing ones to avoid unexpectedly
53
50
// updating the UI.
54
- React . useMemo ( ( ) => {
55
- if ( state ) {
56
- if ( typeof state !== 'object' ) {
57
- return
58
- }
59
-
60
- const queryCache = client . getQueryCache ( )
61
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
62
- const queries = ( state as DehydratedState ) . queries || [ ]
63
-
64
- const newQueries : DehydratedState [ 'queries' ] = [ ]
65
- const existingQueries : DehydratedState [ 'queries' ] = [ ]
66
- for ( const dehydratedQuery of queries ) {
67
- const existingQuery = queryCache . get ( dehydratedQuery . queryHash )
51
+ const hydrationQueue : DehydratedState [ 'queries' ] | undefined =
52
+ React . useMemo ( ( ) => {
53
+ if ( state ) {
54
+ if ( typeof state !== 'object' ) {
55
+ return
56
+ }
68
57
69
- if ( ! existingQuery ) {
70
- newQueries . push ( dehydratedQuery )
71
- } else {
72
- const hydrationIsNewer =
73
- dehydratedQuery . state . dataUpdatedAt >
74
- existingQuery . state . dataUpdatedAt ||
75
- ( dehydratedQuery . promise &&
76
- existingQuery . state . status !== 'pending' &&
77
- existingQuery . state . fetchStatus !== 'fetching' &&
78
- dehydratedQuery . dehydratedAt !== undefined &&
79
- dehydratedQuery . dehydratedAt > existingQuery . state . dataUpdatedAt )
58
+ const queryCache = client . getQueryCache ( )
59
+ // State is supplied from the outside and we might as well fail
60
+ // gracefully if it has the wrong shape, so while we type `queries`
61
+ // as required, we still provide a fallback.
62
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
63
+ const queries = ( state as DehydratedState ) . queries || [ ]
80
64
81
- const queryAlreadyQueued = hydrationQueue ?. find (
82
- ( query ) => query . queryHash === dehydratedQuery . queryHash ,
83
- )
65
+ const newQueries : DehydratedState [ 'queries' ] = [ ]
66
+ const existingQueries : DehydratedState [ 'queries' ] = [ ]
67
+ for ( const dehydratedQuery of queries ) {
68
+ const existingQuery = queryCache . get ( dehydratedQuery . queryHash )
84
69
85
- if (
86
- hydrationIsNewer &&
87
- ( ! queryAlreadyQueued ||
70
+ if ( ! existingQuery ) {
71
+ newQueries . push ( dehydratedQuery )
72
+ } else {
73
+ const hydrationIsNewer =
88
74
dehydratedQuery . state . dataUpdatedAt >
89
- queryAlreadyQueued . state . dataUpdatedAt )
90
- ) {
91
- existingQueries . push ( dehydratedQuery )
75
+ existingQuery . state . dataUpdatedAt ||
76
+ ( dehydratedQuery . promise &&
77
+ existingQuery . state . status !== 'pending' &&
78
+ existingQuery . state . fetchStatus !== 'fetching' &&
79
+ dehydratedQuery . dehydratedAt !== undefined &&
80
+ dehydratedQuery . dehydratedAt >
81
+ existingQuery . state . dataUpdatedAt )
82
+
83
+ if ( hydrationIsNewer ) {
84
+ existingQueries . push ( dehydratedQuery )
85
+ }
92
86
}
93
87
}
94
- }
95
88
96
- if ( newQueries . length > 0 ) {
97
- // It's actually fine to call this with queries/state that already exists
98
- // in the cache, or is older. hydrate() is idempotent for queries.
99
- hydrate ( client , { queries : newQueries } , optionsRef . current )
100
- }
101
- if ( existingQueries . length > 0 ) {
102
- // eslint-disable-next-line react-hooks/react-compiler
103
- setHydrationQueue ( ( prev ) =>
104
- prev ? [ ...prev , ...existingQueries ] : existingQueries ,
105
- )
89
+ if ( newQueries . length > 0 ) {
90
+ // It's actually fine to call this with queries/state that already exists
91
+ // in the cache, or is older. hydrate() is idempotent for queries.
92
+ hydrate ( client , { queries : newQueries } , optionsRef . current )
93
+ }
94
+ if ( existingQueries . length > 0 ) {
95
+ return existingQueries
96
+ }
106
97
}
107
- }
108
- } , [ client , hydrationQueue , state ] )
98
+ return undefined
99
+ } , [ client , state ] )
109
100
110
101
React . useEffect ( ( ) => {
111
102
if ( hydrationQueue ) {
112
103
hydrate ( client , { queries : hydrationQueue } , optionsRef . current )
113
- setHydrationQueue ( undefined )
114
104
}
115
105
} , [ client , hydrationQueue ] )
116
106
0 commit comments