Prefetching with Nextjs cache components, dynamic searchParams, and separate hydration boundaries #11083
Replies: 1 comment
-
|
Great, well-structured question. The most important thing I'd flag is a query-key parity issue between server and client — a few of your other questions resolve themselves once that's fixed. Key parity (your Q8): your server and client build the query key differently, so they can silently diverge. Server-side the key is the resolved Object.fromEntries(useSearchParams().entries())
Object.fromEntries(new URLSearchParams("tag=a&tag=b").entries())
// => { tag: "b" }So One Reuse across boundaries (Q8): yes. Both Simpler pattern (Q7): since both consumers share one query, two Caching the dehydrated state (Q3–Q5): I'd avoid wrapping the whole prefetch+ Resolving searchParams via Hope this helps — curious whether normalizing the key resolves the missed hydration on your end. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I’m trying to use TanStack Query prefetching with Next.js Cache Components while keeping different parts of the page under separate
<Suspense>and<HydrationBoundary>components.The query key depends on
searchParams, so I resolve thesearchParamspromise before passing the resulting plain object into a cached prefetch function.Page implementation
ItemsCountItemsListuseItemsgetQueryClientimplementationEnvironment
Package versions
{ "next": "16.3.0-preview.6", "@tanstack/react-query": "^5.101.2" }next.config.tsGoals
<Suspense>.connection().Questions
searchParamslike this the intended approach?Is it safe and supported to pass the same
Promise<DehydratedState>to multiple<HydrationBoundary>components?Is
"use cache"appropriate around the entire TanStack Query prefetch and dehydration function?Would it be better to put
"use cache"only insidelistItems()and create theQueryClientand dehydrated state outside the cached function?Could caching the dehydrated state cause issues because it contains query timestamps or other TanStack Query metadata?
Can hydrating the same query through multiple boundaries cause duplicated state, overwrites, or unexpected client-side behavior?
Is there a more idiomatic pattern for this case where the query key depends on asynchronous
searchParams, but the page should still preserve streaming?Will both
ItemsCountandItemsListcorrectly reuse the same hydrated query even though they are rendered under separate hydration boundaries?I also opened the same discussion in Nextjs for additional feedback: vercel/next.js#95933
Beta Was this translation helpful? Give feedback.
All reactions