Skip to content

feat: Client router support for API context #3708

Open

Description

@jessypouliot98

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 works

Additional information

No response

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR implementing this feature!

TRP-53

Activity

  1. changed the title [-]feat: Client router support for API context[/-] [+][TRP-53] feat: Client router support for API context[/+] on May 25, 2023
  2. added
    ✅ accepted-PRs-welcome
    and removed on May 25, 2023
  3. changed the title [-][TRP-53] feat: Client router support for API context[/-] [+]feat: Client router support for API context[/+] on May 25, 2023
  4. KATT commented on Nov 2, 2026

    @KATT
    Member

    🤖 In v12, every call takes a headers option that httpLink merges 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.

  5. added
    next-major/definite
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    next-major/definite✅ accepted-PRs-welcome

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions