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

HydrationBoundary.tsrx

83 lines3.1 KB

1// HydrationBoundary β€” hydrates dehydrated query state into the client, then
2// renders its children. Full port of react-query's HydrationBoundary.tsx:
3//
4// - NEW queries hydrate IN RENDER (after SSR, hydration must land before the
5// children render; during a transition, prerender as much as is safe).
6// - EXISTING queries hydrate in an EFFECT: during a transition we must not
7// update live queries/observers until the transition commits β€” an aborted
8// transition must not clobber committed data (octane has transitions, so
9// upstream's abort-safety applies here too).
10// - A dehydrated query counts as NEWER than the cache when its dataUpdatedAt
11// is ahead, OR when it carries a streaming `promise` and was dehydrated
12// after the cached data settled (`dehydratedAt > dataUpdatedAt`) β€” that's
13// what lets later stream chunks re-hydrate an already-settled query.
14import { useRef, useMemo, useEffect } from 'octane';
15import { hydrate } from '@tanstack/query-core';
16import { resolveClient } from './context.ts';
17import type { OctaneNode } from 'octane';
18import type { DehydratedState, HydrateOptions, QueryClient } from '@tanstack/query-core';
19​
20type HydrationBoundaryProps = {
21 state?: unknown;
22 options?: HydrateOptions;
23 queryClient?: QueryClient;
24 children?: OctaneNode;
25};
26​
27export function HydrationBoundary(props: HydrationBoundaryProps) @{
28 const client = resolveClient(props.queryClient);
29 const state = props.state;
30​
31 const optionsRef = useRef(props.options);
32 useEffect(() => {
33 optionsRef.current = props.options;
34 });
35​
36 // "In render" on purpose (the memo is a perf cache, not a semantic barrier) β€”
37 // hydrate new queries now, queue existing ones for the commit effect.
38 const hydrationQueue = useMemo(() => {
39 if (state && typeof state === 'object') {
40 const queryCache = client.getQueryCache();
41 const queries = (state as DehydratedState).queries || [];
42​
43 const newQueries = [];
44 const existingQueries = [];
45 for (const dehydratedQuery of queries) {
46 const existingQuery = queryCache.get(dehydratedQuery.queryHash);
47 if (!existingQuery) {
48 newQueries.push(dehydratedQuery);
49 } else {
50 const hydrationIsNewer =
51 dehydratedQuery.state.dataUpdatedAt > existingQuery.state.dataUpdatedAt ||
52 dehydratedQuery.promise && existingQuery.state.status !== 'pending' &&
53 existingQuery.state.fetchStatus !== 'fetching' &&
54 dehydratedQuery.dehydratedAt !== undefined &&
55 dehydratedQuery.dehydratedAt > existingQuery.state.dataUpdatedAt;
56 if (hydrationIsNewer) {
57 existingQueries.push(dehydratedQuery);
58 }
59 }
60 }
61​
62 if (newQueries.length > 0) {
63 // hydrate() is idempotent for queries β€” safe to run in render.
64 hydrate(client, { queries: newQueries }, optionsRef.current);
65 }
66 if (existingQueries.length > 0) {
67 return existingQueries;
68 }
69 }
70 return undefined;
71 }, [client, state]);
72​
73 // Existing-query hydration lands at commit time (transition abort-safe).
74 useEffect(() => {
75 if (hydrationQueue) {
76 hydrate(client, { queries: hydrationQueue }, optionsRef.current);
77 }
78 }, [client, hydrationQueue]);
79​
80 <>
81 {props.children}
82 </>