UNPKG

@trpc/next

Version:

The tRPC Next.js library

94 lines (71 loc) • 3.82 kB
# 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 From https://trpc.io/docs/client/nextjs/static-pages This page builds on the [App Router setup](https://trpc.io/docs/client/nextjs/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: ```tsx no-check title="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`](https://trpc.io/docs/client/nextjs/app-router-setup#6-create-a-trpc-caller-for-server-components) 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: ```ts no-check title="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, }); ``` ```tsx no-check title="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> ); } ``` ```tsx no-check title="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.