Repository navigation
Conversation
Co-authored-by: alexander <[email protected]>
…_$ensure and pass handles in the setup guides Co-authored-by: alexander <[email protected]>
…the handles guide at the setup's HydrateClient Co-authored-by: alexander <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
6 Skipped Deployments
|
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
…ents handle reader Co-authored-by: alexander <[email protected]>
@trpc/client
@trpc/next
@trpc/openapi
@trpc/react-query
@trpc/server
@trpc/tanstack-react-query
@trpc/upgrade
commit: |
…ined (#7848) <!-- CURSOR_AGENT_PR_BODY_BEGIN --> ## Why - A handle for a procedure without input carried `input: undefined`. - Next.js rejects `undefined` anywhere in `getServerSideProps` / `getStaticProps` props, so this threw: ```ts export async function getServerSideProps() { const viewer = await trpc.viewer.unstable_$ensure(); return { props: { trpcState: dehydrate(queryClient), viewer } }; // Error serializing `.viewer.input` returned from `getServerSideProps` in "/". } ``` ## What - `startHandle` leaves out the `input` key when the input is `undefined`. All of `unstable_$prefetch`, `unstable_$ensure` and `unstable_$fetch`, and the in-render prefetch, go through it. - Before: `{ queryKey: [['viewer'], { type: 'query' }], input: undefined }` - After: `{ queryKey: [['viewer'], { type: 'query' }] }` - Reading `handle.input` still gives `undefined`, so types don't change. The doc comment on `input` now says the key can be absent. - Query keys don't change: the key already left out `undefined` input. - No changeset, because the repo doesn't use changesets. - Follow-up for [#7847](#7847): its Pages Router SSR page has a note about this error. The note can go once both PRs merge. If this PR merges first, #7847 removes the note. ## Links None. No docs or examples change. ## Alternatives considered - Keep the key and tell Pages Router users to work around it (the #7847 note). This loses because the handle is meant to be passed as a prop as is. - Use `null` for "no input". This loses because `null` is valid input for some procedures and would change what `unstable_$fromHandle` passes on. ## Testing - New test in `packages/tanstack-react-query/test/handles.test.tsx`: handles for a no-input procedure, from each verb and from an explicit `undefined`, have only a `queryKey` key. They survive a JSON round trip under `toStrictEqual`. After that round trip, `unstable_$fromHandle` keys still match `queryOptions()` keys, both with and without input. - It failed before the fix (`expected [ 'queryKey', 'input' ] to deeply equal [ 'queryKey' ]`) and passes after. See the two commits. - Checked against Next.js's own `isSerializableProps`: `main` throws `Error serializing .viewer.input`, and this branch returns `true`. - `pnpm test -- --watch false packages/tanstack-react-query`: 1479 tests passed. `tsc --noEmit`, `oxlint` and `oxfmt --check` are clean. ## AI model Claude Opus 5.5 <!-- CURSOR_AGENT_PR_BODY_END --> <div><a href="https://cursor.com/agents/bc-1e6b0138-e595-5202-932f-a58bcf357f2b?cursor_ref=pr_footer&cursor_cta=open_in_web"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-web-light.png"><img alt="Open in Web" width="114" height="28" src="https://cursor.com/assets/images/open-in-web-dark.png"></picture></a> <a href="https://cursor.com/background-agent?bcId=bc-1e6b0138-e595-5202-932f-a58bcf357f2b&cursor_ref=pr_footer&cursor_cta=open_in_cursor"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-cursor-light.png"><img alt="Open in Cursor" width="131" height="28" src="https://cursor.com/assets/images/open-in-cursor-dark.png"></picture></a> </div> <!-- codesmith:footer --> --- <a href="https://backend.blacksmith.sh/track/enable-autofix?expires=1796299078&installation_model_id=443568&pr_number=7848&ref=codesmith_pr_footer&repository=trpc%2Ftrpc&return_to=https%3A%2F%2Fgithub.com%2Ftrpc%2Ftrpc%2Fpull%2F7848&signature=f9042dc3fe5f7b5731189dc96179ac767028b62d4e4a1ab4a4027bb0d2693136"><picture><source media="(prefers-color-scheme: dark)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-light.svg"><img alt="Autofix with [code]smith" src="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark-3x.png" width="190" height="28"></picture></a> <sup><a href="https://app.blacksmith.sh/trpc/codesmith/trpc/pr/7848?autoLogin=true&ref=codesmith_pr_footer">Open in [code]smith</a> · Need help on this PR? Tag <code>@codesmith-bot</code> with what you need.</sup> <!-- codesmith:autofix:disabled --> <!-- /codesmith:footer --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Query handles for procedures without input now contain only the query key, including when called with `undefined`. Handles for procedures with defined input still include their input. * This behavior is preserved when handles are serialized and deserialized. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Cursor Agent <[email protected]>
…ter setup samples (trpc#7849) <!-- CURSOR_AGENT_PR_BODY_BEGIN --> ## Why - Readers of the setup and prefetching guides don't learn that handles exist until they reach the handles guide. [trpc#7847](trpc#7847) rewrites those guides around handles. This PR is the smaller option: the guides keep `queryOptions`, and a few tips point to handles where a page can start its queries early. - The App Router setup's samples typed `~/trpc/server` and `~/trpc/client` with hand-written `declare module` stubs. The setup's router makes `hello` take `{ text }`, but four samples called it with no input, which fails input validation at runtime. The stubs hid that. ## What - Four `:::tip` callouts, each linking to [Prefetching with handles](/docs/client/tanstack-react-query/handles): - TanStack React Query setup, after step 4: start the query in a loader or server component with `unstable_$ensure` and pass the handle, or use `unstable_$prefetch` on `useTRPC()` at the top of the page. - Migrating, after "Migrating Queries": once a component is on `queryOptions`, it can read a handle instead. - Pages Router SSR, after the `getServerSideProps` sample: return a handle instead of the `id`, so the two inputs can't drift apart. - Pages Router server-side helpers, after "Prefetch, fetch and dehydrate": the proxy can return a handle to pass in `props`. - The App Router setup and Server Components pages already had a handles tip, so they get none. - Fixes carried over from [trpc#7847](trpc#7847), without its handles rewrite: - App Router setup: the samples in steps 7 and later share one `twoslash include` built from the setup's real router, query client and server file (`prefetch` and `HydrateClient` as on the page). `trpc.hello.queryOptions()` and `caller.hello()` now pass `{ text: 'world' }`. - Server Components: the `useQuery` sample switches on `greetingQuery.status` instead of checking `!greeting.data`, and the `useSuspenseQuery` sample names its result `greetingQuery` instead of destructuring `data`. - Usage: the quick example's component is `Greeting`, not `Users`, and renders `greetingQuery.data`. - `www/build/collapse.ts`: the Shiki transformer is named `trpc-docs:collapse`, like the others, not `raf-query-docs:collapse`. - Not carried over: anything that turns a sample into a handles sample. ## Links - [TanStack React Query setup](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/tanstack-react-query/setup) - [4. Fetch data](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/tanstack-react-query/setup#4-fetch-data): new tip. - [Migrating](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/tanstack-react-query/migrating) - [Migrating Queries](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/tanstack-react-query/migrating#migrating-queries): new tip. - [Pages Router SSR](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/nextjs/pages-router/ssr) - [Prefetch in `getServerSideProps`](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/nextjs/pages-router/ssr#prefetch-in-getserversideprops): new tip. - [Pages Router server-side helpers](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/nextjs/pages-router/server-side-helpers) - [Prefetch, fetch and dehydrate](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/nextjs/pages-router/server-side-helpers#prefetch-fetch-and-dehydrate): new tip. - [Next.js App Router setup](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/nextjs/app-router-setup) - [7. Make API requests](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/nextjs/app-router-setup#7-make-api-requests), [Leveraging Suspense](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/nextjs/app-router-setup#leveraging-suspense) and [Getting data in a Server Component](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/nextjs/app-router-setup#getting-data-in-a-server-component): samples checked against the real router, with `{ text: 'world' }` where it was missing. - [Server Components](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/tanstack-react-query/server-components) - [Using your API](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/tanstack-react-query/server-components#using-your-api): the client component switches on `status`. - [Leveraging Suspense](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/tanstack-react-query/server-components#leveraging-suspense): the query result is named `greetingQuery`. - [Usage](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/tanstack-react-query/usage) - [Quick example query](https://www-git-cursor-docs-handles-tips-trpc.vercel.app/docs/client/tanstack-react-query/usage#quick-example-query): `Greeting` renders the data. ## Alternatives considered - [trpc#7847](trpc#7847): every setup guide reads handles. It teaches the new pattern first, but it changes many samples at once while the APIs are still `unstable_`. - A tip on every page that prefetches, such as SSG and the Next.js overview: more tips than readers need. SSR and server-side helpers cover the Pages Router. ## Testing - `pnpm --filter www check-twoslash`: 462 blocks compile, including the App Router samples against the real router. - `pnpm validate-skills`: all 24 skills pass, including the size limit on the generated Pages Router skill. - `pnpm --filter www test` (314 tests), `pnpm --filter www lint`, `pnpm typecheck-www` and `pnpm --filter www build` pass. All 698 trpc.io URLs work. - In the built HTML, each new tip renders as a `callout-tip` aside. - Handles fit in Pages Router `props`: a handle is a plain `{ queryKey, input }` object, and since [trpc#7848](trpc#7848) it leaves out `input` when it's `undefined`. ## AI model Claude Opus 5.5 <!-- CURSOR_AGENT_PR_BODY_END --> <div><a href="https://cursor.com/agents/bc-d9d00df6-3344-5394-89b4-74130061fc31?cursor_ref=pr_footer&cursor_cta=open_in_web"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-web-light.png"><img alt="Open in Web" width="114" height="28" src="https://cursor.com/assets/images/open-in-web-dark.png"></picture></a> <a href="https://cursor.com/background-agent?bcId=bc-d9d00df6-3344-5394-89b4-74130061fc31&cursor_ref=pr_footer&cursor_cta=open_in_cursor"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-cursor-light.png"><img alt="Open in Cursor" width="131" height="28" src="https://cursor.com/assets/images/open-in-cursor-dark.png"></picture></a> </div> <!-- codesmith:footer --> --- <a href="https://backend.blacksmith.sh/track/enable-autofix?expires=1796302697&installation_model_id=443568&pr_number=7849&ref=codesmith_pr_footer&repository=trpc%2Ftrpc&return_to=https%3A%2F%2Fgithub.com%2Ftrpc%2Ftrpc%2Fpull%2F7849&signature=088413ffc68c65697ecaf0a6ba9688e786a595e34d1aa13739eda2e5eb2dae9e"><picture><source media="(prefers-color-scheme: dark)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-light.svg"><img alt="Autofix with [code]smith" src="https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark-3x.png" width="190" height="28"></picture></a> <sup><a href="https://app.blacksmith.sh/trpc/codesmith/trpc/pr/7849?autoLogin=true&ref=codesmith_pr_footer">Open in [code]smith</a> · Need help on this PR? Tag <code>@codesmith-bot</code> with what you need.</sup> <!-- codesmith:autofix:disabled --> <!-- /codesmith:footer --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Documentation** * Expanded guidance for using prefetched query handles with Next.js pages and TanStack React Query, including passing handles to components and reading the associated queries. * Clarified how query handles compare with existing prefetching and fetching methods, and when cached data must be available. * Updated App Router and React Query examples with consistent inputs and clearer loading, error, and success states. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Cursor Agent <[email protected]>
|
This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you. |
Why
useQuery(trpc.x.queryOptions(input))called ad hoc in components. A server or loader prefetch then had to repeat the same procedure and input, and nothing checked that they matched.unstable_$ensure(orunstable_$prefetch), and the component reads the handle withtrpc.unstable_$fromHandle(handle). The data is in the cache before render, and the read can't drift from what was started.queryOptions.What
trpc/client.tsxexportstype TRPC = TRPCOptionsProxy<AppRouter>.trpc/server.tsxusesunstable_createHydrateClient(getQueryClient)instead of a hand-writtenHydrateClientandprefetchhelper.await trpc.hello.unstable_$ensure(input)and pass the handle. Client components read it withuseQuery(trpc.unstable_$fromHandle(props.greeting)).unstable_$prefetchanduseSuspenseQuery.unstable_$ensureplusensureQueryData, notfetchQuery.declare modulestubs.getServerSideProps/getStaticPropsreturn the handle fromunstable_$ensurenext totrpcState, and the page reads it. The 404 example catches the rejection fromunstable_$ensure. The setup page's mistakes list says to pass handles instead of repeating the query.UserCardreads a handle. It's fed by a React Router loader withunstable_$ensure, or in render withunstable_$prefetchwhen there's no loader.unstable_$ensureand a component reading the handle. The rest of the page, which documents the options and keys, is unchanged.TRPCandunstable_createHydrateClient.abortOnUnmountpages, the waterfalls demo, v9/v10 docs and blog posts.www/build/collapse.ts: the Shiki transformer is namedtrpc-docs:collapse, like the others (trpc-docs:code-meta,trpc-docs:type-queries).Before (
pages-router/ssr.md):After:
Checks run locally:
pnpm --filter www check-twoslash: 461 blocks in 102 files compile.pnpm typecheck-www,pnpm --filter www testandpnpm --filter www lintpass.pnpm validate-skillspasses. The generatednextjs-pages-routerskill is 494 lines, under the 500-line limit.pnpm --filter www buildpasses: all 698 URLs work.getServerSidePropspasses Next.js'sisSerializablePropscheck and reads back after hydrating. A handle for a procedure without an input carriesinput: undefined, which Next.js rejects. The SSR page notes this.Links
Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.Summary by CodeRabbit