This integration automates SSR dehydration/hydration and streaming between TanStack Router and TanStack Query. If you haven't read the standard External Data Loading guide, start there.
What you get
- Automatic SSR dehydration/hydration of your QueryClient
- Streaming of queries that resolve during initial server render to the client
- Redirect handling for redirect() thrown from queries/mutations
- Optional provider wrapping with QueryClientProvider
Installation
The TanStack query integration is a separate package that you need to install:
Loading...
Setup
Create your router and wire up the integration. Ensure a fresh QueryClient is created per request in SSR environments.
Loading...
By default, the integration wraps your router with a QueryClientProvider. If you already provide your own provider, pass wrapQueryClient: false and keep your custom wrapper.
SSR behavior and streaming
- During server render, the integration dehydrates initial queries and streams any subsequent queries that resolve while rendering.
- On the client, the integration hydrates the initial state, then incrementally hydrates streamed queries.
- Queries from useSuspenseQuery or loader prefetches participate in SSR/streaming. Plain useQuery does not execute on the server.
Use in routes
Using useSuspenseQuery vs useQuery
- useSuspenseQuery: runs on the server during SSR when its data is required and will be streamed to the client as it resolves.
- useQuery: does not execute on the server; it will fetch on the client after hydration. Use this for data that is not required for SSR.
// Suspense: executes on server and streams
const { data } = useSuspenseQuery(postsQuery)
// Non-suspense: executes only on client
const { data, isLoading } = useQuery(postsQuery)
Loading...
Preload with a loader and read with a hook
Preload critical data in the route loader to avoid waterfalls and loading flashes, then read it in the component. The integration ensures server-fetched data is dehydrated and streamed to the client during SSR.
Loading...
Prefetching and streaming
You can also prefetch with fetchQuery or ensureQueryData in a loader without consuming the data in a component. If you return the promise directly from the loader, it will be awaited and thus block the SSR request until the query finishes. If you don't await the promise nor return it, the query will be started on the server and will be streamed to the client without blocking the SSR request.
Loading...
Redirect handling
If a query or mutation throws a redirect(...), the integration intercepts it on the client and performs a router navigation.
- Enabled by default
- Disable with handleRedirects: false if you need custom handling
Works with TanStack Start
TanStack Start uses TanStack Router under the hood. The same setup applies, and the integration will stream query results during SSR automatically.