Skip to content
tRPC

Partners

Static pages and metadata

Share data between generateMetadata and the page, and prerender or revalidate (ISR) App Router pages with a context that reads no request data

On this page

This page builds on the App Router setup.

Data for generateMetadata

generateMetadata and the page render in the same request, so getQueryClient() returns the same query client in both. Fetch in generateMetadata, and the page's prefetch of the same query reuses the data instead of calling the procedure again:

app/page.tsx
import { getQueryClient, HydrateClient, trpc } from '~/trpc/server';
import { ClientGreeting } from './client-greeting';

export async function generateMetadata() {
  const greeting = await getQueryClient().fetchQuery(
    trpc.hello.queryOptions({ text: 'world' }),
  );
  return { title: greeting.greeting };
}

export default async function Home() {
  // Fresh within `staleTime`, so this reuses what `generateMetadata` fetched
  await getQueryClient().prefetchQuery(trpc.hello.queryOptions({ text: 'world' }));

  return (
    <HydrateClient>
      <ClientGreeting />
    </HydrateClient>
  );
}

Static and ISR pages

The context in trpc/server.tsx reads headers(), so a page that loads data through trpc renders on every request, whatever its revalidate or generateStaticParams. Next.js finds out during the build: headers() throws an error with a digest that tells Next.js to render the page on demand.

To prerender a page and revalidate it (ISR), load its data with a second proxy whose context reads no request data:

trpc/server.tsx
// For static and ISR pages: a context without the request's headers
export const trpcStatic = createTRPCOptionsProxy({
  ctx: () => createTRPCContext({ headers: new Headers() }),
  router: appRouter,
  queryClient: getQueryClient,
});
app/greeting/[text]/page.tsx
import { getQueryClient, HydrateClient, trpcStatic } from '~/trpc/server';
import { ClientGreeting } from './client-greeting';

// Render the page again at most once a minute
export const revalidate = 60;

// Prerender these at build time; others render on their first visit
export function generateStaticParams() {
  return [{ text: 'world' }];
}

export default async function Page(props: { params: Promise<{ text: string }> }) {
  const { text } = await props.params;
  // `fetchQuery` throws if the procedure fails, unlike `prefetchQuery`: the build
  // fails, and a failed revalidation keeps serving the last good page
  await getQueryClient().fetchQuery(trpcStatic.hello.queryOptions({ text }));

  return (
    <HydrateClient>
      <ClientGreeting text={text} />
    </HydrateClient>
  );
}
app/greeting/[text]/client-greeting.tsx
'use client';

import { useSuspenseQuery } from '@tanstack/react-query';
import { useTRPC } from '~/trpc/client';

export function ClientGreeting(props: { text: string }) {
  const trpc = useTRPC();
  const greetingQuery = useSuspenseQuery(trpc.hello.queryOptions({ text: props.text }));
  return <div>{greetingQuery.data.greeting}</div>;
}
  • Procedures get a context built without the request's headers, so anything your context reads from them, such as the signed-in user, is missing. Only public data belongs on these pages.
  • Don't use trpc and trpcStatic on the same page: they share the query client and the query keys.
  • The route's layouts must not read request data either.
  • generateStaticParams runs at build time, outside any request. If it reads its params from a procedure, use trpcStatic there too.