What this thread does
The problem
An agent writes every Next.js data fetch with TanStack Query in client components, or the opposite, throws it out because "Server Components replace it". Both extremes hurt: a client-fetching page shows spinners for data that could be in the first HTML, while a filter-heavy dashboard loses caching and refetching.
What the thread says
A r/nextjs poster (October 2026) asked whether TanStack Query is still reasonable on Next 16 for a login-protected dashboard with filters, charts and mutations over a separate backend. The top answers said it is a good fit: Server Components do not replace client caching, refetching and invalidation; use RSC where data is of immediate value and client queries where data is conditional or interactive. Tips repeated by several commenters:
- Do not create the
QueryClientat module scope, or cache can leak between requests on the server. Commenters suggested auseStatein a client provider; TanStack's guide adds a caveat: avoiduseStatefor it when there is no Suspense boundary between the provider and suspending code, because React can discard the client during a suspended first render. ItsgetQueryClient()pattern makes a new client per request on the server and reuses one in the browser. - Put URL search params in the query key so a filter change is just a new key.
- Use
placeholderData: keepPreviousDataso tables and charts do not flash empty on filter change;useInfiniteQueryfor load-more. - Set
staleTimeabove 0 so hydration is not followed by an instant refetch. - Do not fetch the same data in a Server Component and again in a client query.
One commenter raised a limit: with HydrationBoundary the server re-fetches on every navigation even if the client cache is fresh, so Next's server-first model fits less well than a framework with client-side loaders. Their point is a trade-off, not a verdict.
What the official guide says
TanStack's advanced SSR guide says: use a getQueryClient() that makes a new client per request on the server and reuses one in the browser; set staleTime above 0; wrap client components in <HydrationBoundary state={dehydrate(queryClient)}>; prefetch in Server Components but do not render query results there, since they can drift when the client revalidates; since v5.40 streamed prefetches use shouldDehydrateQuery; use Server Actions for mutations, not as the queryFn.
Prompt to try (mine, untried)
"For each data need in this page, say whether it should be fetched in a Server Component or with useQuery, and why. For useQuery data, add a per-request getQueryClient, prefetch on the server with HydrationBoundary, staleTime 60 seconds, and search params in the query key."
Limits
Community advice plus docs; I did not run this setup.
Sources
Primary source, opened 18 October 2026: TanStack Query advanced SSR guide.
- Confirms: a new
QueryClientper request on the server and one reused in the browser;staleTimeabove 0 to avoid an immediate client refetch;HydrationBoundarywithdehydrate; prefetch in Server Components but do not render the query result there, because it can drift when the client revalidates; from v5.40.0 pending queries can be streamed withshouldDehydrateQuery; Server Actions suit mutations but are not recommended insidequeryFnbecause they run serially. - Corrected above: the guide does not simply say "use
useState". It warns thatuseStatecan lose the client if React discards a suspended first render when no Suspense boundary sits between the provider and the suspending code. - Also checked on November 15, 2026: the query-key guide and paginated-query guide support the search-params-in-key and
keepPreviousDatatips. Not checked here: the commenter's claim about re-fetching on every navigation (community opinion).
TanStack Query in Next.js App Router: per-request QueryClient, HydrationBoundary, staleTime. Packaged by WhatWorks editors. Original post