Guides
Server rendering and hydration
How the server's query cache streams to the browser, pending queries included, and how the browser reconciles it with its own cache.
With server rendering, the queries a page needs run on the server, and the HTML arrives with the data already in it. raf-query streams the server's cache to the browser as entries start and settle, so the browser starts with the same data and doesn't fetch it again.
Your components never deal with this. A handle only carries a key and an input, and the browser's cache has the entry by the time anything reads it. There is no initialData to thread through props, and no hydration boundary to place around each component.
What happens on a request
- The server creates a query client for the request. Each request gets its own, so users never see each other's data.
- Loaders or server components call
load.swrandrevalidateentries wait for their data;prefetchentries don't. - React renders and streams HTML. Components reading a pending query suspend, and their Suspense fallbacks go out first.
- The cache streams alongside the HTML. Each entry is sent once, as soon as it starts or settles. Entries still pending cross as promises, which resolve in the browser when the server's request finishes.
- The browser hydrates. Its query client receives each entry. A pending entry resumes the server's request instead of starting a new one, so a query never runs twice.
- The request ends. The server's client is cleared and dropped.
Server clients
createQueryClient makes a server client where window doesn't exist (set isServer to choose). A server client renders once, so it behaves differently:
| Behavior | Browser | Server |
|---|---|---|
| Retries | With retry() | None, even with retry() |
gcTime | 5 minutes by default | Never collected: the cache lives as long as the request |
| Stale cached data | Refetched (swr entries resolve with it and refetch alongside) | Reused: staleTime doesn't apply within a request; revalidate entries refetch invalidated data |
| Focus and reconnect | Refetch read, stale queries, with focusRefetch() and reconnectRefetch() | Not watched: features start when a provider mounts |
So in a server render, every entry makes at most one request per query, whatever its staleTime, unless a write invalidates the query and a revalidate entry reads it again.
A server render fetches a query that nothing started, except where server components feed it: see Queries nothing started on the server.
The provider
Every framework provides the client with QueryClientProvider. One Providers component works in all of them: it takes the router's client when the router makes one, and otherwise creates its own, one per server render and one per browser tab:
"use client";
import { type interface QueryClient<TTypes extends RootTypes = { readonly "~options": {}; readonly context: {}; readonly options: {}; }>A query cache. TTypes is its root's types: what its plugins declare. It
runs the factories of builders bound to those plugins, or to fewer of them,
so the bare QueryClient type takes any client.
QueryClient, function QueryClientProvider(props: {
readonly children?: ReactNode;
readonly client: QueryClient;
}): JSX.Element
Provides a client to the tree, and runs its features (focus and
reconnect refetching, polling, …) while mounted.
QueryClientProvider } from "@raf-query/react";
import { type type ReactNode = string | number | bigint | boolean | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode> | ReactPortal | Promise<AwaitedReactNode> | null | undefinedRepresents all of the things React can render.
ReactNode, function useState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>] (+1 overload)Returns a stateful value, and a function to update it.
useState } from "react";
import { function makeQueryClient(): QueryClient<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
makeQueryClient } from "~/query-client.ts";
/**
* The app's query client: the router's when it makes one, or one created
* here, per server render and per browser tab.
*/
export function function Providers(props: {
readonly children: ReactNode;
readonly client?: QueryClient;
}): JSX.Element
The app's query client: the router's when it makes one, or one created
here, per server render and per browser tab.
Providers(props: {
readonly children: ReactNode;
readonly client?: QueryClient;
}
props: {
readonly children: ReactNodechildren: type ReactNode = string | number | bigint | boolean | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode> | ReactPortal | Promise<AwaitedReactNode> | null | undefinedRepresents all of the things React can render.
ReactNode;
readonly client?: QueryClient<{
readonly "~options": {};
readonly context: {};
readonly options: {};
}> | undefined
client?: interface QueryClient<TTypes extends RootTypes = { readonly "~options": {}; readonly context: {}; readonly options: {}; }>A query cache. TTypes is its root's types: what its plugins declare. It
runs the factories of builders bound to those plugins, or to fewer of them,
so the bare QueryClient type takes any client.
QueryClient;
}) {
const [const client: QueryClient<{
readonly "~options": {};
readonly context: {};
readonly options: {};
}>
client] = useState<QueryClient<{
readonly "~options": {};
readonly context: {};
readonly options: {};
}>>(initialState: QueryClient<{
readonly "~options": {};
readonly context: {};
readonly options: {};
}> | (() => QueryClient<{
readonly "~options": {};
readonly context: {};
readonly options: {};
}>)): [QueryClient<{
readonly "~options": {};
readonly context: {};
readonly options: {};
}>, Dispatch<SetStateAction<QueryClient<{
readonly "~options": {};
readonly context: {};
readonly options: {};
}>>>] (+1 overload)
Returns a stateful value, and a function to update it.
useState(() => props: {
readonly children: ReactNode;
readonly client?: QueryClient;
}
props.client?: QueryClient<{
readonly "~options": {};
readonly context: {};
readonly options: {};
}> | undefined
client ?? function makeQueryClient(): QueryClient<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
makeQueryClient());
return (
<function QueryClientProvider(props: {
readonly children?: ReactNode;
readonly client: QueryClient;
}): JSX.Element
Provides a client to the tree, and runs its features (focus and
reconnect refetching, polling, …) while mounted.
QueryClientProvider client: QueryClient<{
readonly "~options": {};
readonly context: {};
readonly options: {};
}>
client={const client: QueryClient<{
readonly "~options": {};
readonly context: {};
readonly options: {};
}>
client}>{props: {
readonly children: ReactNode;
readonly client?: QueryClient;
}
props.children: ReactNodechildren}</function QueryClientProvider(props: {
readonly children?: ReactNode;
readonly client: QueryClient;
}): JSX.Element
Provides a client to the tree, and runs its features (focus and
reconnect refetching, polling, …) while mounted.
QueryClientProvider>
);
}| Framework | Renders |
|---|---|
| TanStack Start | <Providers client={queryClient}> in the router's Wrap, with the client loaders use |
| Next.js | <Providers> in the root layout. Server components use their own client per request |
| React Router | <Providers> in the root route. Loaders use the request's client from middleware |
"use client" makes it a client component in Next.js. Elsewhere the directive does nothing.
Queries nothing started on the server
A component can read a query that no loader or server component started: a waterfallHandle's query, or one the server's cache dropped after its loader ran. What the server render does with it depends on what carries the server's cache to the browser:
| Framework | A read of a query nothing started, on the server |
|---|---|
| TanStack Start | Fetches while rendering. setupRouterIntegration streams the entry with the rest of the cache, so the browser resumes it instead of fetching again |
| React Router | Fetches while rendering, so the HTML has the data. A loader's snapshot carries only what loaders started, so the browser fetches the query again |
| Next.js and other RSC frameworks | Renders the nearest Suspense fallback, and the query loads in the browser |
With server components, the client components' server render has nothing to send its own requests through: only server components' HydrationBoundary sends entries. So that HydrationBoundary marks the client components' server render as it renders. Below it, a read of a query nothing sent doesn't start the request: its read rejects, use() throws, and React renders the nearest Suspense fallback in the HTML. The browser renders that boundary again, and its read starts the query there. Put a Suspense boundary around such readers, or start the query in a server component to have it in the HTML.
React sees that read as an error it recovers from. The error has the digest Next.js uses for ssr: false, so Next.js reports nothing. Other RSC frameworks report it to the server render's onError and, while hydrating, the browser's onRecoverableError.
The mark is set when the first HydrationBoundary renders. A client component that renders before it, such as one in a root layout above the page's boundary, still fetches on the server, and the browser fetches that query again.
Errors
The stream sends entries with data or with a request in flight. A query that had already failed when its entry would have been sent stays on the server, and the browser fetches it again when something reads it.
A query sent while pending crosses as a promise. If it throws on the server, the promise rejects with a RedactedError that names the query but not the message, so server internals (SQL errors, stack traces) don't leak into the page. The browser treats it like any failed load: the reader throws to its error boundary, and resetting the boundary with resetOne refetches the query from the browser. Pass keepErrors: true in the integration's dehydrateOptions to send the original error instead.
Browser-only queries
Some data only the browser can know: a push subscription, a notification permission, a service worker, a media query. That still belongs in the cache, with the same factory, handle and invalidation. Mark its factory ssr: false:
export function function pushStateQuery(input: {
slug: string;
}): QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
pushStateQuery(input: {
slug: string;
}
input: { slug: stringslug: string }) {
return queryOptions<readonly ["pushState", string], "on" | "off", "on" | "off">(options: BaseOptions<readonly ["pushState", string]> & Partial<{
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | false | undefined;
}> & {
readonly queryFn: (context: QueryFunctionContext<readonly ["pushState", string], {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
};
readonly context: {};
readonly options: {
...;
};
}>) => Awaitable<...>;
readonly select?: ((data: "on" | "off") => "on" | "off") | undefined;
}): QueryOptions<...> (+1 overload)
queryOptions({
BaseOptions<readonly ["pushState", string]>.queryKey: readonly ["pushState", string]A plain array, usually built by a group(). Handles keep its literal type.
queryKey: const pushState: Group
<readonly [string]>(rest_0: string) => readonly ["pushState", string]
pushState(input: {
slug: string;
}
input.slug: stringslug),
queryFn: (context: QueryFunctionContext<readonly ["pushState", string], {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>) => Awaitable<...>
queryFn: () => function detectPushState(slug: string): Promise<"on" | "off">Whether this browser gets a push when the post gets a new comment.
detectPushState(input: {
slug: string;
}
input.slug: stringslug),
// Only the browser can tell: the server never runs it.
CoreOptions.ssr?: boolean | undefinedfalse for a query only a browser can run (a permission, the service
worker, localStorage): a server client never starts it, never sends
it, and reads it as not loaded yet, so the browser runs it once it
renders. Default true.
ssr: false,
});
}
A loader starts it like any other query, with prefetch:
export async function function loader(slug: string): Promise<{
readonly push: QueryHandle<(input: {
slug: string;
}) => QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>>;
}>
loader(slug: stringslug: string) {
const [const handles: {
readonly push: QueryHandle<(input: {
slug: string;
}) => QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>>;
}
handles] = await load<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}, {
...;
}>(client: QueryClient<...>, entries: {
...;
} & {
...;
}): Promise<...>
Start a loader's queries and return a serializable handle for each. It
starts every entry before it waits for any, so the order of the entries
can't create a waterfall. Resolves with [handles, data] once every
swr and revalidate entry has data, and rejects with the error as soon
as one fails.
prefetch entries never make it reject. A skipToken in place of an
entry starts nothing and gets no handle.
load(const queryClient: QueryClient<{
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
queryClient, {
push: LoadEntry<(input: {
slug: string;
}) => QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>, "prefetch">
push: prefetch<(input: {
slug: string;
}) => QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>>(factory: (input: {
slug: string;
}) => QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>, ...args: LoadEntryArgs<...>): LoadEntry<...>
Start the query if the cache has no fresh data, and don't wait for it.
It never makes the load reject: errors show up where a component reads
the handle.
prefetch(function pushStateQuery(input: {
slug: string;
}): QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
pushStateQuery, { slug: stringslug }),
});
return const handles: {
readonly push: QueryHandle<(input: {
slug: string;
}) => QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>>;
}
handles;
}
- On the server, a client never starts it:
prefetchreturns its handle and starts nothing, and nothing is sent for it.swrandrevalidatereject, since the server can never have its data (unless something wrote it withsetData). - In the browser,
prefetchstarts it on a client-side navigation, alongside the route's other queries. After a server render, no loader runs, so the first read starts it.
Read it without suspending, so the server renders a placeholder and the browser fills it in:
function function PushSettings(props: {
handle: QueryHandle<typeof pushStateQuery>;
}): React.JSX.Element
PushSettings(props: {
handle: QueryHandle<typeof pushStateQuery>;
}
props: { handle: QueryHandle<(input: {
slug: string;
}) => QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>>
handle: interface QueryHandle<TFactory extends QueryFactory = (input: unknown) => { readonly queryFn: (context: never) => unknown; readonly queryKey: QueryKey; }>A small serializable reference to a query: its key and its factory's
input. It carries no data; readers get that from the cache. Handles of
different factories have different types.
QueryHandle<typeof function pushStateQuery(input: {
slug: string;
}): QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
pushStateQuery> }) {
const [const pushThenable: QueryValue<(input: {
slug: string;
}) => QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>>
pushThenable] = useQuery<(input: {
slug: string;
}) => QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>>(factory: (input: {
slug: string;
}) => QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>, handle: QueryHandle<...>, options?: UseQueryOptions): readonly [...]
Read a query by its handle.
useQuery(function pushStateQuery(input: {
slug: string;
}): QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
pushStateQuery, props: {
handle: QueryHandle<typeof pushStateQuery>;
}
props.handle: QueryHandle<(input: {
slug: string;
}) => QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>>
handle);
// "unknown" on the server and until the browser has detected it.
const const push: "on" | "off" | "unknown"push =
const pushThenable: QueryValue<(input: {
slug: string;
}) => QueryOptions<readonly ["pushState", string], "on" | "off", "on" | "off", {
readonly "~options": {
readonly retry: (value: number | false | undefined) => number | false | undefined;
readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
};
readonly context: {};
readonly options: {
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>>
pushThenable.status: "rejected" | "pending" | "fulfilled"status === "fulfilled" ? const pushThenable: Promise<"on" | "off"> & {
readonly status: "fulfilled";
readonly value: "on" | "off";
}
pushThenable.value: "on" | "off"value : "unknown";
return <function PushSwitch(props: {
readonly state: "on" | "off" | "unknown";
}): ReactNode
PushSwitch state: "on" | "off" | "unknown"state={const push: "on" | "off" | "unknown"push} />;
}
On the server, the thenable is "rejected" with the error React uses to render a boundary in the browser instead (digest BAILOUT_TO_CLIENT_SIDE_RENDERING). A use() of it renders the nearest Suspense fallback, and the browser renders the content once the query loads. Next.js reports nothing for that error. Other frameworks report it to the server render's onError, and an error outside every Suspense boundary fails the render, so put such a reader under one.
When the browser already has data
On client-side navigations in Next.js, server components render again and send their cache entries with the RSC payload, and React Router's loaders return a new snapshot, while the browser may already have the same entries. The browser takes whichever copy is newer, by when it was fetched (updatedAt; server and browser clocks should agree):
- No data, no request of its own: it resumes the server's pending request.
- Data, or its own request in flight: it waits for the server's copy and takes it if newer.
- If it keeps its own copy (the server's is older, as in a payload the router replays from its cache on back and forward, or the server's request failed), it refetches it if a component reads it and it's stale.
Serialization
Data, keys and inputs cross the wire, so they must be serializable by the framework's serializer. TanStack Start uses Seroval, Next.js uses React's RSC serialization, and React Router uses turbo-stream: all three handle Date, Map, Set, BigInt and promises. meta stays on the server: the browser reads it from its own factories.
Only send what's serializable. These docs, for instance, cache each demo's code module as a query, and a module full of components can't cross the wire. The router integration's filter option keeps those entries out of the stream. A filter replaces the default one, canDehydrate (entries with data or a request in flight), so combine the two:
// Every demo query's key starts with this group.
const const demos: Group<readonly ["demos"]>demos = group<"demos">(name: "demos"): Group<readonly ["demos"]> (+1 overload)A group, or a subgroup inside parent. Call it for a query's key: a
flat, plain array that starts with the group's path. Invalidating a group
covers its subgroups and every query below them.
group("demos");
setupRouterIntegration<AnyRouter>(options: RouterIntegrationOptions<AnyRouter>): voidConnects a query client to a TanStack Router (and TanStack Start) router:
setupRouterIntegration({
RouterIntegrationOptions<TRouter extends AnyRouter>.dehydrateOptions?: DehydrateOptions | undefineddehydrateOptions: {
DehydrateOptions.filter?: ((entry: Entry) => boolean) | undefinedWhich entries to send. Default: entries with data or a request in
flight. An entry with neither has nothing to send and stays behind.
filter: (entry: Entryentry) => entry: Entryentry.Entry.key: QueryKeykey[0] !== const demos: Group<readonly ["demos"]>demos.Group<readonly ["demos"]>.path: readonly ["demos"]Its parents' names and its own.
path[0] && function canDehydrate(entry: Entry): booleanThe default filter: entries with data or a request in flight.
canDehydrate(entry: Entryentry),
},
RouterIntegrationOptions<TRouter extends AnyRouter>.queryClient: QueryClient<{
readonly "~options": {};
readonly context: {};
readonly options: {};
}>
queryClient,
RouterIntegrationOptions<AnyRouter>.router: AnyRouterrouter,
});Integrations
- TanStack Start:
setupRouterIntegrationstreams the router's query client. - React Server Components:
HydrationBoundaryfor the Next.js App Router and other RSC frameworks. - React Router:
HydrationBoundaryfor the snapshot each Framework Mode loader returns.
Without an integration, @raf-query/react has the pieces they use: dehydrate(client) snapshots the cache, dehydrateStream(client) snapshots it and then streams entries as they start and settle, and hydrate(client, state) writes a snapshot into a client. HydrationBoundary takes a snapshot as its state prop and hydrates it while rendering.