Repository navigation
Open
Labels
next-major/definiteDefinite things for next major✅ accepted-PRs-welcomeFeature proposal is accepted and ready to work on
Description
Describe the feature you'd like to request
It's currently "impossible" to get the request's cookies in the tRPC client router (createTRPCProxyClient) in Next.js
In order to setup Auth inside getServerSideProps with tRPC we need to be able to forward the initial requests headers to that proxy client.
For now I found that this works, but is not ideal
export const createClient = (ctx?: Pick<NextPageContext, 'req' | 'res'>) => {
const headers = getServerHeaders(ctx);
return createTRPCProxyClient<AppRouter>({
transformer: superjson,
links: [
splitLink({
condition(op) {
// check for context property `skipBatch`
return op.context.skipBatch === true;
},
// when condition is true, use normal request
true: httpLink({
url: `${getBaseUrl()}/api/trpc`,
headers: () => headers,
}),
// when condition is false, use batching
false: httpBatchLink({
url: `${getBaseUrl()}/api/trpc`,
headers: () => headers,
}),
}),
],
})
};export const getServerSideProps = getSSRProps(async (ctx) => {
const client = createClient(ctx);
const user = await client.auth.getUser.query();Describe the solution you'd like to see
Like createTRPCNext where we can get the ctx.req from the config callback like so
export const trpc = createTRPCNext<AppRouter>({
config: ({ ctx }) => ({ /* Insert custom config */ })
});Describe alternate solutions
An alternate solution would be to pass the next gssp context in the query params
const foo = client.some.router.query({/* some params */}, { ctx: nextContext }); // Note I see that `context` already exists in there, but couldn't figure out what it does/how it worksAdditional information
No response
👨👧👦 Contributing
- 🙋♂️ Yes, I'd be down to file a PR implementing this feature!
Metadata
Metadata
Assignees
Labels
next-major/definiteDefinite things for next major✅ accepted-PRs-welcomeFeature proposal is accepted and ready to work on
Activity
[-]feat: Client router support for API context[/-][+][TRP-53] feat: Client router support for API context[/+][-][TRP-53] feat: Client router support for API context[/-][+]feat: Client router support for API context[/+]KATT commentedon Nov 2, 2026
🤖 In v12, every call takes a
headersoption thathttpLinkmerges into that request, so a server-side client can forward the incoming request's cookies per call, e.g.client.post.list.query(input, { headers: { cookie } }), without a link per request. https://v12.trpc.io/v12/docs/client/overview#call-options. v12 isn't released yet and there's no ETA.