# Prefetching with handles

> 

Handles help you avoid request waterfalls. The loader, or server component, **reads the URL once**, starts every query the page needs, and passes each component a reference to its query as a prop.

- A **handle** is a query's key and input. It holds no data.
- The component reads the query from it, fully typed, without naming the procedure or repeating its input.
- The client reads exactly the query the server started. With plain `queryOptions`, an input that differs by a typo misses the prefetch and fetches again, silently.

> [!NOTE] Info
> The handle APIs are plain functions from `@trpc/tanstack-react-query/unstable-handles`. The `unstable-` in the path means they're new and may change, but you're safe to use them! [Read more](https://trpc.io/docs/faq.md#unstable).


## In short {#gist}

Load the handles where the page starts, and read each one in a component, which never names the procedure or repeats its input:

<div class="code-tabs" data-tabs="framework">
<div data-tab="TanStack Start">

```tsx title="src/routes/posts.$id.tsx"
import { useSuspenseQuery } from '@tanstack/react-query';
import { createFileRoute } from '@tanstack/react-router';
import {
  ensureEntry,
  fromHandle,
  loadHandles,
} from '@trpc/tanstack-react-query/unstable-handles';
import { useTRPC } from '~/lib/trpc';

export const Route = createFileRoute('/posts/$id')({
  loader: async ({ context: { trpc }, params }) => {
    const [handles] = await loadHandles(trpc, {
      post: ensureEntry(trpc.post.byId, { id: params.id }),
    });
    return handles;
  },
  component: PostPage,
});

function PostPage() {
  const handles = Route.useLoaderData();
  const trpc = useTRPC();
  const postQuery = useSuspenseQuery(fromHandle(trpc, handles.post));
  return <h1>{postQuery.data.title}</h1>;
}
```

</div>
<div data-tab="Next.js">

The page, a server component, loads the handles:

```tsx title="app/posts/[id]/page.tsx"
import { ensureEntry, loadHandles } from '@trpc/tanstack-react-query/unstable-handles';
import { HydrateClient, trpc } from '~/trpc/server';
import { Post } from './post';

export default async function PostPage(props: { params: Promise<{ id: string }> }) {
  const { id } = await props.params;
  const [handles] = await loadHandles(trpc, {
    post: ensureEntry(trpc.post.byId, { id }),
  });
  return (
    <HydrateClient>
      <Post post={handles.post} />
    </HydrateClient>
  );
}
```

A client component, in its own file, reads one:

```tsx title="app/posts/[id]/post.tsx"
'use client';

import { useSuspenseQuery } from '@tanstack/react-query';
import { fromHandle, type inferQueryHandle } from '@trpc/tanstack-react-query/unstable-handles';
import { useTRPC, type TRPC } from '~/trpc/client';

export function Post(props: { post: inferQueryHandle<TRPC['post']['byId']> }) {
  const trpc = useTRPC();
  const postQuery = useSuspenseQuery(fromHandle(trpc, props.post));
  return <h1>{postQuery.data.title}</h1>;
}
```

</div>
</div>

- The handle carries the procedure and its input, typed.
- The data reaches the browser's cache through TanStack Query's dehydrate and hydrate: see [Frameworks](#frameworks).
- Mutations don't change. To refetch a handle's query after one, invalidate `{ queryKey: handle.queryKey, exact: true }`.

## Loading handles {#starting-a-query}

`loadHandles(trpc, entries)` starts every entry, then resolves to `[handles, data]`:

```ts
import {
  ensureEntry,
  loadHandles,
  prefetchInfiniteEntry,
} from '@trpc/tanstack-react-query/unstable-handles';

const [handles, data] = await loadHandles(trpc, {
  // Waits for the post, unless the cache has it
  post: ensureEntry(trpc.post.byId, { id }),
  // Doesn't wait
  comments: prefetchInfiniteEntry(trpc.post.comments, { postId: id }),
});
handles.post;
// (property) post: TRPCQueryHandle<{
//     input: {
//         id: string;
//     };
//     output: Post;
//     transformer: false;
//     errorShape: DefaultErrorShape;
//     featureFlags: {
//         keyPrefix: false;
//     };
// }>
data.post;
// (property) post: Post
// The comments may have no data yet, so they aren't on `data`
data.comments;
```

- Building an entry starts nothing. The load starts every entry before it waits for any, so their order doesn't matter.
- `handles` has a handle for each entry. Return it from the loader, or pass it on as props.
- In a client component, don't call `loadHandles` during render: each render would start the entries again. Server components can. A client component that renders before anything loaded its query reads it with `useSuspenseQuery(trpc.x.queryOptions(input))`.

#### Using the data in a loader {#loader-data}

`data` has the data of the entries the load waited for. Use it for the page title, a redirect or a not-found check. Return `handles`, not `data`, so the data doesn't cross to the browser twice.

#### Waiting for some, streaming others {#combining-both}

List every query in one load, whether the page waits for it or not. They all start together. A second load waits for everything the first one waited for, so use one only for [dependent queries](#dependent-queries).

Each entry chooses whether the page waits for its query:

|                        | `prefetchEntry`                   | `ensureEntry`                                 | `fetchEntry`          |
| ---------------------- | --------------------------------- | --------------------------------------------- | --------------------- |
| Page waits for data    | No                                | Yes                                           | Yes                   |
| In `data`              | No                                | Yes                                           | Yes                   |
| With stale data        | Refetches in the background       | Resolves at once, refetches in the background | Waits for the refetch |
| Reader's first render  | Loading state, or cached data     | The data                                      | The data              |
| When the request fails | The reader gets the error         | The load rejects, unless it had cached data   | The load rejects      |



Load handles where the code can wait:

| Your app                       | Where                      | Proxy                                   |
| ------------------------------ | -------------------------- | --------------------------------------- |
| TanStack Start, TanStack Router | Route loaders             | The proxy in your router context        |
| Next.js App Router             | Server components          | The server proxy from `trpc/server.tsx` |
| A SPA without loaders          | Event handlers, such as a link's hover | `useTRPC()`                 |

#### Conditional queries {#conditional-queries}

Put TanStack Query's `skipToken` in place of an entry to start nothing for it. Here, a post page loads its comments only when the URL opens the comments tab, `/posts/1?tab=comments`:

```tsx
import { skipToken } from '@tanstack/react-query';
import {
  ensureEntry,
  loadHandles,
  prefetchInfiniteEntry,
} from '@trpc/tanstack-react-query/unstable-handles';
import { z } from 'zod';

export const postRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: '/posts/$id',
  validateSearch: z.object({
    tab: z.enum(['post', 'comments']).default('post'),
  }),
  loaderDeps: ({ search }) => ({ tab: search.tab }),
  loader: async ({ context: { trpc }, params: { id }, deps }) => {
    const [handles] = await loadHandles(trpc, {
      post: ensureEntry(trpc.post.byId, { id }),
      comments:
        deps.tab === 'comments'
          ? prefetchInfiniteEntry(trpc.post.comments, { postId: id })
          : skipToken,
    });
    handles.comments;
    // (property) comments: TRPCInfiniteQueryHandle<{
    //     input: {
    //         postId: string;
    //         limit?: number | undefined;
    //         cursor?: number | null | undefined;
    //     };
    //     output: {
    //         items: {
    //             id: string;
    //             text: string;
    //         }[];
    //         nextCursor: number | null;
    //     };
    //     transformer: false;
    //     errorShape: DefaultErrorShape;
    //     featureFlags: {
    //         keyPrefix: false;
    //     };
    // }> | undefined
    return handles;
  },
});
```

- Its handle is `undefined`, so render the reader only when there's a handle: `{handles.comments && <Comments comments={handles.comments} />}`.
- It has no key in `handles`, `data` or `loadHandlesSettled`'s results, so the handles still serialize as Next.js props.
- Take the condition from what the loader knows: the URL, as here, the session, or another load's data. See [Start every query from the URL](#url-as-input) and [Dependent queries](#dependent-queries).

#### TanStack Query equivalents {#tanstack-equivalents}

Each entry is one TanStack Query call:

| tRPC                                                                 | TanStack Query                                                                                                                                                           |
| -------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `prefetchEntry(trpc.x, input, opts)`                                 | `void queryClient.prefetchQuery(trpc.x.queryOptions(input, opts))`                                                                                                       |
| `ensureEntry(trpc.x, input, opts)`                                   | `await queryClient.ensureQueryData({ ...trpc.x.queryOptions(input, opts), revalidateIfStale: true })`                                                                    |
| `fetchEntry(trpc.x, input, opts)`                                    | `await queryClient.fetchQuery(trpc.x.queryOptions(input, opts))`                                                                                                         |
| `prefetchInfiniteEntry`, `ensureInfiniteEntry`, `fetchInfiniteEntry` | `prefetchInfiniteQuery`, `ensureInfiniteQueryData` (with `revalidateIfStale: true`), `fetchInfiniteQuery`                                                                |
| `fromHandle(trpc, handle, opts)`                                     | `trpc.x.queryOptions(handle.input, opts)`, with `x` taken from the handle                                                                                                |

The `queryClient` is the one the proxy was created with. The entry names follow the TanStack Query method each one calls.

Options:

- `input` is optional when the procedure's is: `prefetchEntry(trpc.viewer)`.
- `opts` takes `staleTime`, `gcTime`, `retry`, `retryDelay`, `meta` and `trpc` request options. They apply to the load's request; readers use the options passed to `fromHandle`.

### `prefetchEntry`: don't wait {#prefetch}

- The page renders at once, and the data streams in.
- The reader shows cached data, or suspends. Give it its own `<Suspense>`, or the nearest boundary above shows its fallback, which may be the whole page.
- It never makes the load reject. The reader gets the error: see [Errors](#errors).
- Use it for slow queries, content below the fold and side panels.

### `ensureEntry`: wait {#ensure}

- The load waits until the cache has data, so the reader renders it at once.
- With cached data, even stale, it resolves at once and refetches in the background.
- It rejects only if the cache had no data and the request fails.
- Use it for data the page can't render without, or needs before rendering.

### `fetchEntry`: wait for fresh data {#fetch}

- Like `ensureEntry`, but with stale data it waits for the refetch.
- Use it when the loader reads the data and needs it fresh, such as for a redirect.
- In a browser loader, prefer `ensureEntry`: a fetch entry holds up the navigation until the refetch is done.
- TanStack Router shows a visited page with its last loader data while the loader reruns. To wait, set `defaultStaleReloadMode`, or a loader's [`staleReloadMode`](https://tanstack.com/router/latest/docs/guide/data-loading#choosing-background-vs-blocking-stale-reloads), to `'blocking'`.
- In server components, ensure and fetch behave the same: the request's `QueryClient` starts empty.

## Start every query from the URL {#url-as-input}

A loader runs before anything renders, and all it has is the URL: params, search params and, on the server, the session. If every input comes from those, every request starts with the navigation.

- **Path params** name the thing: `/posts/$slug`.
- **Search params** hold filters, sort and page. Keep them out of component state: a loader can't see it, the back button doesn't restore it, and a shared link loses it.

```
/posts?sort=top&page=2  →  prefetchEntry(trpc.post.list, { sort: 'top', page: 2 })
```

With handles, they all start together. Without, each starts once the component above it renders:

```
With handles      Without
|-> post.byId     |-> post.byId
|-> post.comments   |-> post.comments
|-> post.byAuthor     |-> post.byAuthor
```



### If the URL isn't enough, reshape the procedure {#reshape-procedures}

A procedure that takes an id from another query forces a waterfall on every page that calls it. Fix the procedure, not the page:

- **Take what the URL has.** Accept the slug and look up the id on the server.
- **Return the joined data.** Return the post with its author, not an author id.

The server sits next to the database, so the lookup costs a query there, not a round trip from the browser.

**Before:** the comments and the author need ids that only the post has.

```ts title="The loader for /posts/$slug"
async function loader(params: { slug: string }) {
  const [first, { post }] = await loadHandles(trpc, {
    post: ensureEntry(trpc.post.bySlug, { slug: params.slug }),
  });
  // Nothing below can start until the post has arrived
  const [second] = await loadHandles(trpc, {
    comments: prefetchInfiniteEntry(trpc.post.comments, {
      postId: post.id,
    }),
    author: prefetchEntry(trpc.author.byId, { id: post.authorId }),
  });
  return { ...first, ...second };
}
```

```
post.bySlug     |-------|
post.comments           |-------|
author.byId             |-------|
```

**After:** `post.comments` takes the slug, and `post.bySlug` returns the author:

```ts title="server/routers/_app.ts"
import { z } from 'zod';
import { db } from './db';
import { t } from './trpc';

export const appRouter = t.router({
  post: t.router({
    bySlug: t.procedure
      .input(z.object({ slug: z.string() }))
      .query(async ({ input }) => {
        const post = await db.post.bySlug(input.slug);
        return { ...post, author: await db.author.byId(post.authorId) };
      }),
    comments: t.procedure
      .input(
        z.object({ slug: z.string(), cursor: z.number().nullish() }),
      )
      .query(async ({ input }) => {
        const post = await db.post.bySlug(input.slug);
        return db.comment.list({
          postId: post.id,
          cursor: input.cursor ?? null,
        });
      }),
  }),
});
```

```ts title="The loader for /posts/$slug"
async function loader(params: { slug: string }) {
  // Both start from the URL, together
  const [handles] = await loadHandles(trpc, {
    comments: prefetchInfiniteEntry(trpc.post.comments, {
      slug: params.slug,
    }),
    post: ensureEntry(trpc.post.bySlug, { slug: params.slug }),
  });
  return handles;
}
```

```
post.bySlug     |-------|
post.comments   |-------|
```

## Reading a handle {#reading-a-handle}

`fromHandle(trpc, handle, opts?)` returns `trpc.<path>.queryOptions(handle.input, opts)`, or `infiniteQueryOptions` for an infinite handle. Pass it to any TanStack Query hook:

```tsx
const trpc = useTRPC();
const postQuery = useSuspenseQuery(fromHandle(trpc, props.post));
const titleQuery = useQuery(fromHandle(trpc, props.post, { select: (post) => post.title }));
const [aQuery, bQuery] = useSuspenseQueries({
  queries: [fromHandle(trpc, props.a), fromHandle(trpc, props.b)],
});
```

- Only `loadHandles` makes handles. A hand-written object doesn't type-check, and `fromHandle` throws on it.
- `handle.input` is the typed input. An infinite handle's leaves out `cursor` and `direction`.
- Reads share cache entries with `trpc.<path>.queryOptions(input)`.
- `handle.queryKey` carries no data type, so the handle stays serializable. For a typed key, use `fromHandle(trpc, handle).queryKey` or `trpc.post.byId.queryKey(handle.input)`.

#### Typing props {#typing-props}

Type props with `inferQueryHandle<TRPC['post']['byId']>`, or `inferInfiniteQueryHandle` for an infinite handle. Export `type TRPC = TRPCOptionsProxy<AppRouter>` from `trpc/client.tsx` once.

#### Reading with `useQuery` {#use-query}

With `useQuery`, no `Suspense` boundary is needed:

- It renders `status: 'pending'` until the data arrives, and `'success'` at once after an ensure or fetch entry.
- With server rendering, read a prefetch entry's handle with `useSuspenseQuery`. Otherwise the server can render the loading state while the browser has the data, a hydration mismatch ([TanStack/query#9399](https://github.com/TanStack/query/issues/9399)).

To change the input in the component, for a search term the URL can't hold, call the procedure and start from the handle's input:

```tsx
const [term, setTerm] = useState(props.comments.input.term);
const commentsQuery = useSuspenseInfiniteQuery(
  trpc.post.comments.infiniteQueryOptions(
    { ...props.comments.input, term: useDeferredValue(term) },
    { getNextPageParam: (last) => last.nextCursor },
  ),
);
```

- While the input matches the handle's, it reads the loader's cache entry.
- Render `<Comments key={postId} … />`, so a new post resets the term.
- For sort and filters, use a [search param](#url-as-input) instead.

## When the cache loses the data {#cache-loses-data}

A handle carries no data, and the cache can lose it even after an ensure entry:

- `resetQueries()` clears it and refetches at once.
- `removeQueries()` and `clear()`, such as on logout, drop it. The reader loads it again when it next renders.
- After `gcTime` unused, TanStack Query drops it.
- `invalidateQueries()` keeps it and refetches in the background, so nothing shows a loading state.

So give every reader a loading state: a `Suspense` boundary, or a `'pending'` case.

## Infinite queries {#infinite-queries}

For procedures with a `cursor` input, use `prefetchInfiniteEntry`, `ensureInfiniteEntry` and `fetchInfiniteEntry`. They load one page, or more with `pages`:

```ts
const [handles, data] = await loadHandles(trpc, {
  comments: ensureInfiniteEntry(
    trpc.post.comments,
    { postId: id, limit: 20 },
    { pages: 3, getNextPageParam: (last) => last.nextCursor },
  ),
});
data.comments.pages;
// (property) InfiniteData<{ items: { id: string; text: string; }[]; nextCursor: number | null; }, number | null>.pages: {
//     items: {
//         id: string;
//         text: string;
//     }[];
//     nextCursor: number | null;
// }[]
```

- `pages` is how many pages the load fetches, also when it refetches stale pages: the cache then keeps only that many. To refetch every page the reader loaded, leave it out.
- Functions can't cross to the client, so pass `getNextPageParam` again when you read:

  ```ts
  fromHandle(trpc, props.comments, { getNextPageParam: (last) => last.nextCursor })
  ```

- Also pass it to entries in loaders that run in the browser. Refetching stale pages needs it from page two on.
- Type props with `inferInfiniteQueryHandle`.

## Dependent queries {#dependent-queries}

A query whose input comes from another query's result is a waterfall. Each step costs a round trip. First try to [reshape the procedure](#reshape-procedures). If you can't, there are two ways, and both pay the round trip.

#### In a loader {#waterfall-in-a-loader}

Load the first query, then build the second load from its `data`:

```ts
const [first, { post }] = await loadHandles(trpc, {
  post: ensureEntry(trpc.post.byId, { id }),
  comments: prefetchInfiniteEntry(trpc.post.comments, { postId: id }),
});
const [second] = await loadHandles(trpc, {
  moreByAuthor: prefetchInfiniteEntry(trpc.post.byAuthor, {
    authorId: post.authorId,
  }),
});
const handles = { ...first, ...second };
```

#### In a component {#waterfall-in-a-component}

Read it in the component that gets the input:

```tsx
const postsQuery = useSuspenseInfiniteQuery(
  trpc.post.byAuthor.infiniteQueryOptions(
    { authorId },
    { getNextPageParam: (last) => last.nextCursor },
  ),
);
```

- In TanStack Router, `beforeLoad` can wait for the first query before the loader runs: see [TanStack Start](https://trpc.io/docs/client/tanstack-react-query/tanstack-start.md#before-load).

## Frameworks {#frameworks}

Each framework loads the handles where the page starts and passes them to the components that read them. Set tRPC up with the framework's guide first.

<div class="code-tabs" data-tabs="framework">
<div data-tab="TanStack Start">

#### On a page {#tanstack-start}

After the [TanStack Start setup](https://trpc.io/docs/client/tanstack-react-query/tanstack-start.md), the route's loader loads the handles and its component passes them on:

```tsx title="src/routes/posts.$id.tsx"
import { createFileRoute } from '@tanstack/react-router';
import {
  ensureEntry,
  loadHandles,
  prefetchInfiniteEntry,
} from '@trpc/tanstack-react-query/unstable-handles';
import { Suspense } from 'react';
import { Comments } from '~/components/comments';
import { Post } from '~/components/post';

export const Route = createFileRoute('/posts/$id')({
  loader: async ({ context: { trpc }, params }) => {
    const [handles] = await loadHandles(trpc, {
      post: ensureEntry(trpc.post.byId, { id: params.id }),
      comments: prefetchInfiniteEntry(trpc.post.comments, { postId: params.id }),
    });
    return handles;
  },
  component: PostPage,
});

function PostPage() {
  const handles = Route.useLoaderData();
  return (
    <>
      <Post post={handles.post} />
      <Suspense fallback={<p>Loading comments…</p>}>
        <Comments comments={handles.comments} />
      </Suspense>
    </>
  );
}
```

</div>
<div data-tab="Next.js">

#### On a page {#nextjs-app-router}

After the [Next.js App Router setup](https://trpc.io/docs/client/nextjs/app-router-setup.md), export `HydrateClient = unstable_createHydrateClient(getQueryClient)` from `trpc/server.tsx`. The page, a server component, loads the handles and passes them to client components:

```tsx title="app/posts/[id]/page.tsx"
import {
  ensureEntry,
  loadHandles,
  prefetchInfiniteEntry,
} from '@trpc/tanstack-react-query/unstable-handles';
import { Suspense } from 'react';
import { HydrateClient, trpc } from '~/trpc/server';
import { Comments } from './comments';
import { Post } from './post';

export default async function PostPage(props: { params: Promise<{ id: string }> }) {
  const { id } = await props.params;
  const [handles] = await loadHandles(trpc, {
    post: ensureEntry(trpc.post.byId, { id }),
    comments: prefetchInfiniteEntry(trpc.post.comments, { postId: id }),
  });
  return (
    <HydrateClient>
      <Post post={handles.post} />
      <Suspense fallback={<p>Loading comments…</p>}>
        <Comments comments={handles.comments} />
      </Suspense>
    </HydrateClient>
  );
}
```

```tsx title="app/posts/[id]/post.tsx"
'use client';

import { useSuspenseQuery } from '@tanstack/react-query';
import { fromHandle, type inferQueryHandle } from '@trpc/tanstack-react-query/unstable-handles';
import { useTRPC, type TRPC } from '~/trpc/client';

export function Post(props: { post: inferQueryHandle<TRPC['post']['byId']> }) {
  const trpc = useTRPC();
  const postQuery = useSuspenseQuery(fromHandle(trpc, props.post));
  return <h1>{postQuery.data.title}</h1>;
}
```

#### `HydrateClient` {#hydrate-client}

- It sends the request's queries to the browser, pending ones too, which stream in as they settle.
- It sends each query once per request, so a nested server component can load more handles and render its own. A query is sent again only when the server has newer data.
- It works only in server components, and for `queryClient.prefetchQuery` too.
- Handle inputs cross through React's serialization, not your transformer. Keep them to JSON values and `Date`s.
- For a 404, use an ensure entry and call `notFound()`: see [Errors](#errors).

#### Metadata, static and ISR pages {#nextjs-static}

- `generateMetadata` gets the page's query client. Fetch there, and the page's ensure entry reuses the data.
- A page whose tRPC context reads `headers()` renders on every request. `loadHandles` lets Next.js see that, so the build marks the page dynamic.
- To prerender the page and revalidate it, load it with a proxy whose context reads no request data, `trpcStatic` from [Static and ISR pages](https://trpc.io/docs/client/nextjs/static-pages.md#static-pages):

```tsx title="app/posts/[id]/page.tsx"
import { ensureEntry, loadHandles } from '@trpc/tanstack-react-query/unstable-handles';
import { getQueryClient, HydrateClient, trpcStatic } from '~/trpc/server';
import { Post } from './post';

type Props = { params: Promise<{ id: string }> };

export const revalidate = 60;

export function generateStaticParams() {
  return [{ id: '1' }];
}

export async function generateMetadata(props: Props) {
  const { id } = await props.params;
  const post = await getQueryClient().fetchQuery(
    trpcStatic.post.byId.queryOptions({ id }),
  );
  return { title: post.title };
}

export default async function PostPage(props: Props) {
  const { id } = await props.params;
  const [handles] = await loadHandles(trpcStatic, {
    post: ensureEntry(trpcStatic.post.byId, { id }),
  });
  return (
    <HydrateClient>
      <Post post={handles.post} />
    </HydrateClient>
  );
}
```

</div>
</div>

## Errors {#errors}

#### With ensure and fetch entries {#errors-ensure}

When an entry the load waits for fails, `loadHandles` rejects with a `TRPCEntryError`. `isTRPCEntryError(error, trpc.post.byId)` checks which procedure failed and types `error.cause`:

```ts
import {
  ensureEntry,
  ensureInfiniteEntry,
  isTRPCEntryError,
  loadHandles,
} from '@trpc/tanstack-react-query/unstable-handles';

async function loader(id: string) {
  const [handles] = await loadHandles(trpc, {
    post: ensureEntry(trpc.post.byId, { id }),
    comments: ensureInfiniteEntry(trpc.post.comments, { postId: id }),
  }).catch((error: unknown) => {
    if (isTRPCEntryError(error, trpc.post.byId) && error.code === 'NOT_FOUND') {
      notFound();
    }
    if (isTRPCEntryError(error, trpc.post.comments)) {
      reportError(error.cause);
      // (parameter) error: TRPCEntryError<{
      //     input: {
      //         postId: string;
      //         limit?: number | undefined;
      //         cursor?: number | null | undefined;
      //     };
      //     output: {
      //         items: {
      //             id: string;
      //             text: string;
      //         }[];
      //         nextCursor: number | null;
      //     };
      //     transformer: false;
      //     errorShape: DefaultErrorShape;
      //     featureFlags: {
      //         keyPrefix: false;
      //     };
      // }>
    }
    throw error;
  });
  return handles;
}
```

- `error.key` is the entry's key, `error.path` the procedure, `error.code` the tRPC code.
- The load rejects on the first failure, without waiting for the others.
- An error with a `digest`, such as the one Next.js's `headers()` throws in a static render, is rethrown as it is, also by `loadHandlesSettled`, so Next.js still sees it. A procedure's errors arrive as a `TRPCError`, as with `fetchQuery`.

#### Errors as values {#errors-as-values}

`loadHandlesSettled` never rejects for a failed entry. It returns each waited-for entry's result as a value:

```ts
import {
  ensureEntry,
  loadHandlesSettled,
  prefetchInfiniteEntry,
} from '@trpc/tanstack-react-query/unstable-handles';

const [handles, results] = await loadHandlesSettled(trpc, {
  post: ensureEntry(trpc.post.byId, { id }),
  comments: prefetchInfiniteEntry(trpc.post.comments, { postId: id }),
});
if (results.post.status === 'error') {
  if (results.post.error.code === 'NOT_FOUND') {
    notFound();
  }
  throw results.post.error;
}
results.post.data.title;
// (property) data: Post
```

#### With prefetch entries {#errors-prefetch}

A prefetch entry never makes the load reject. The reader gets the query's own error: `useQuery` returns `status: 'error'`, and `useSuspenseQuery` throws to the nearest error boundary, unless the cache has data to keep showing. To report these in one place, use your `QueryCache`'s [`onError`](https://tanstack.com/query/latest/docs/reference/QueryCache).

## Invalidation {#invalidation-and-staleness}

Handles use normal tRPC query keys. Pass any of these to `invalidateQueries`:

- `{ queryKey: handle.queryKey, exact: true }`: the handle's own query. Without `exact`, it also matches inputs that extend the handle's.
- `trpc.post.pathFilter()`: every query under `post`.
- `trpc.post.byId.queryFilter(input)`, or `infiniteQueryFilter` for an infinite handle.

To update data in place, use `queryClient.setQueryData(trpc.post.byId.queryKey(handle.input), …)`.

Hydrated data keeps the server's fetch time. Set a `staleTime` above 0, as the setup guides do, or the client refetches right after hydration.

## Testing {#testing}

Load handles over a test `QueryClient`, seeded with fixture data:

```tsx title="post.test.tsx"
import { QueryClient } from '@tanstack/react-query';
import { createTRPCOptionsProxy } from '@trpc/tanstack-react-query';
import { loadHandles, prefetchEntry } from '@trpc/tanstack-react-query/unstable-handles';
import { appRouter } from './router';

const queryClient = new QueryClient({
  defaultOptions: { queries: { staleTime: Infinity, retry: false } },
});
const trpc = createTRPCOptionsProxy({ router: appRouter, ctx: {}, queryClient });

queryClient.setQueryData(trpc.post.byId.queryKey({ id: '1' }), {
  id: '1',
  title: 'Hello',
});
const [{ post }] = await loadHandles(trpc, {
  post: prefetchEntry(trpc.post.byId, { id: '1' }),
});
```

- Render the component under `QueryClientProvider` and `TRPCProvider` with the same `queryClient`.
- Leave out `setQueryData` to run the real procedure.

## FAQ {#faq}

#### Are handles safe to send to the browser? {#handles-safe}

Yes. A handle names a query the browser could call anyway, and every refetch runs validation and auth on the server again.

A handle does carry its input to the browser, as hydrated query keys already do. Only use inputs you'd show the client.

#### Where don't handles work? {#where-handles-dont-work}

- **Server functions and server actions:** their `QueryClient` never hydrates the page, so return data instead.
- **Outside hydration:** a reader rendered on the server outside `HydrateClient` or `HydrationBoundary` fetches its query again. In development, tRPC warns once per query.
- **URLs and storage:** a handle there gains nothing over its input.

#### Why functions, not methods on `trpc`? {#why-functions}

Methods on `trpc` return plain TanStack Query options. Handles are helpers on top, and apps that don't use them don't load their code.

#### Why one load call, not one per query? {#why-one-load}

So a loader can't make a waterfall by the order of its lines.
