HydrationBoundary ignores/skips server-prefetched query when a parent useQuery with same key is rendered before child useSuspenseQuery
Maintainer antworten meist innerhalb von 1 Tag
@mgarcialeniolabs arbeitet bereits daran.
Seit 27.8.2026.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 52/100
- Issue-Typ
- Bug
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- react, typescript
- Bereich
- frontend
Rechercherichtung
Beginne mit der verknüpften CodeSandbox-Reproduktion und verfolge das Verhalten von HydrationBoundary im Umfeld von useQuery im Parent und useSuspenseQuery im Child anhand des im Bericht beschriebenen Ablaufs in app/(dashboard)/todos/[todoId]/layout.tsx oder page.tsx. Vergleiche die Zustände von serverseitigem Prefetching, Dehydration und Hydration; abgeschlossen ist die Aufgabe, wenn der prefetched key für das Child verfügbar bleibt, ohne eine doppelte SSR-Anfrage oder einen client-request guard error zu verursachen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Describe the bug
If a useQuery is rendered before a HydrationBouhndary, hydrations for that key will be skipped despite the fact that the useQuery would have not fetched in the first place.
In our case we have a prefetch where we pass the dehydrated queryClient to a HydrationBoundary. But there is a useQuery using the same key as the prefetched query above the hydration boundary that (in some global component higher up) gets rendered. The hydration boundary skips this hydration and the child components of the HydrationBoundary that have useSuspenseQuery for that key still run their queries in SSR.
Small example:
function Page({ params }) {
const queryClient = getQueryClient();
void queryClient.prefetchQuery(
buildTodoQueryOptions(params.todoId, makeServerRequest),
);
return (
<>
<SomeHeaderComponent /> {/* a `useQuery` lives in here */}
<HydrationBoundary state={dehydrate(queryClient)}>
<MyChildComponent /> {/* a `useSuspenseQuery` lives in here and fires despite the above prefetch */}
</HydrationBoundary>
</>
);
}
Also, in our particular case this triggers a guard we have to ensure we do not client fetch on the server during SSR. This also means the request is being duplicated and firing twice (in our prefetch and during SSR, which should have been skipped)
Flow (all on the server):
- Parent/route boundary prefetches the detail query
todoIdon the server. - A higher component in layout/header renders a
useQuerywith the keytodoId. - Pass the dehydrated and prefetched
queryClientto aHydrationBoundarylower than theuseQuery. - The HydrationBoundary` skips this key and does not hydrate the prefetched query as expected.
- Child
useSuspenseQuerythen attempts a fetch with client requestor during SSR and hits the guard error.
Repro
Server prefetch at detail layout/page:
// In app/(dashboard)/todos/[todoId]/layout.tsx or page.tsx
function Page({ params }) {
const queryClient = getQueryClient();
void queryClient.prefetchQuery(buildTodoQueryOptions(todoId, makeServerRequest));
return <HydrationBoundary state={dehydrate(queryClient)}>{children}</HydrationBoundary>;
}
Global/header client component that also reads URL todoId (lives OUTSIDE/above the above layout.tsx)
"use client";
import { useQuery, useParams } from "@tanstack/react-query";
function TodoHeaderMenu() {
const params = useParams();
const todoId = typeof params.todoId === "string" ? params.todoId : "";
const { data: todo } = useQuery({
...buildTodoQueryOptions(todoId),
enabled: Boolean(todoId),
});
return <span>{todo?.title}</span>;
}
Child detail component:
"use client";
function TodoPage({ todoId }: { todoId: string }) {
const { data: todo } = useSuspenseQuery(buildTodoQueryOptions(todoId));
return <TodoDetails todo={todo} />;
}
buildTodoQueryOptions defaults to client requestor. Client-request guard throws:
if (typeof window === "undefined") {
throw new Error(
"Warning, aborting SSR. You attempted to fetch from the server with clientRequest."
);
}
AI disclosure: I had codex help me with the quick repro.
Questions
- Is this current behavior expected?
- Should hydration logic be changed to only skip queries that are fetching (aka don't skip
useQueriesthat fill the cache key on the server but do not initiate filling the value on the server)
Your minimal, reproducible example
https://codesandbox.io/p/devbox/pr7c4g
Steps to reproduce
The home page on that will bring up the error in the next dev error popup
Expected behavior
I would expect a useQuery in a higher component with the same key to not STOP the server fetch/hydration given useQuery would not trigger the client request guard like useSuspenseQuery does. useQuery does not start the queryFn on the server, so it should not stop useSuspenseQueries that CAN imo.
This also comes with the additional problem that because of the useQuery, the useSuspenseQuery is firing again, ruining deduplication.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
macOS
Chromium
next: 16.1.1
Tanstack Query adapter
react-query
TanStack Query version
5.90.21
TypeScript version
No response
Additional context
I can technically gate the useQuery in the parent component with something like
const isDealQueryReady = !!(
state &&
state.data !== undefined &&
state.status !== "pending" &&
state.fetchStatus !== "fetching"
)
//and then
if (!isDealQueryReady) {
return null;
}
//...
//...`useQuery`
and then I do not get the error but that is obnoxious.
- Vorherrschende Sprache
- TypeScript
- Sterne
- 50.4k
- Forks
- 4.3k
- Ø Merge
- 12 Std. 18 Min.
- Gemergte PRs (30 T.)
- 488
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus TanStack/query
-
QueryClient.isFetching and useIsFetching accept a fetchStatus filter and ignore itEvtl. vergeben @Apu-Emdad hat das vor 4 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
solid-query: STRICT_READ_UNTRACKED on Solid 2 — client()/options() read in component body (useMutation, useBaseQuery)Evtl. wieder frei @LeonxLJX hat das vor 44 Tagen übernommen, und es ist kein Pull Request offen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
TanStack/query#11358 · 2 Kommentare · 1 Reaktion ·
Maintainer antworten meist innerhalb von 1 Tag
-
solid-query 6.0.0-rc.5: onCacheEvent throws NotReadyError during a Solid 2 (rc.14+) transition landingEvtl. vergeben @UzumakiODZ hat das vor 3 Tagen übernommen. Offen
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 28/100
TanStack/query#12031 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Explicit gcTime schedules a GC timer during SSR, retaining the whole render in memoryEvtl. vergeben @AdzerKI hat das vor 4 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 25/100
TanStack/query#12025 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
restoreQueries and persisterGc throw on malformed persisted entriesEvtl. vergeben @VGontier-cmd hat das vor 12 Tagen übernommen. Offen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
paperclipai/paperclip#16785 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
code-yeongyu/senpi#3661 ·
Maintainer antworten meist innerhalb von 1 Tag
-
community first-timers-only good first issue hacktoberfest help wanted low hanging fruit up-for-grabs
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 78/100
lingdojo/kana-dojo#32731 · 1 Kommentar · 5 Reaktionen ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug status: needs triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
mastra-ai/mastra#27538 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Yeachan-Heo/oh-my-codex#3822 ·
Maintainer antworten meist innerhalb von 1 Tag