Skip to content

fix(server): keep Set-Cookie headers set before responseMeta - #8217

Merged
trpc-bot merged 1 commit into
trpc:mainfrom
sagarrsharmaa:fix/keep-set-cookie-headers
Nov 27, 2026
Merged

trpc-bot merged 1 commit into
trpc:mainfrom
sagarrsharmaa:fix/keep-set-cookie-headers

Conversation

@sagarrsharmaa

@sagarrsharmaa sagarrsharmaa commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Why

  • In the Node adapters (standalone, Express, Next.js API routes), a cookie that a middleware or procedure set on res is dropped when responseMeta also sets a cookie.
  • With res.cookie('session', …) in an Express middleware and set-cookie: theme=dark from responseMeta, the client only gets theme=dark.

What

  • The Node adapters add responseMeta's cookies after the ones already on res instead of replacing them.
  • If responseMeta sets no cookie, nothing changes. Fastify already appends cookies, so it is unchanged.

Links

None

Alternatives considered

  • res.appendHeader(): shorter, but HTTP/2 responses only have it from Node 20.12, and some hosts pass partial response objects. getHeader plus setHeader works everywhere.

Testing

  • Regression check: packages/tests/server/regression/nodeSetCookieHeaders.test.ts sets a cookie in a middleware and one in a procedure, on a standalone and an Express server, next to a responseMeta cookie. On main only the responseMeta cookie comes back.

AI model

Opus 5.5


Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Summary by CodeRabbit

  • Bug Fixes
    • HTTP responses now retain cookies set earlier in the request when additional cookies are added later. This prevents middleware, endpoint, and response-level cookies from being lost and ensures they are all included in the final response.
    • Verified cookie handling across the standalone and Express server adapters.

In the Node adapters (standalone, Express, Next.js API routes), a cookie
that a middleware or procedure set on `res` was lost when `responseMeta`
also returned a `Set-Cookie` header. For example, with an Express
middleware calling `res.cookie('session', ...)` and `responseMeta`
returning `set-cookie: theme=dark`, the client only got `theme=dark`.

`writeResponse` wrote the response's cookies with
`res.setHeader('set-cookie', cookies)`, which replaces the header. It now
adds them after the cookies already on `res`. When `responseMeta` sets
no cookie, nothing changes. The Fastify adapter already appends cookies.
@sagarrsharmaa
sagarrsharmaa requested a review from a team as a code owner November 27, 2026 13:44
@vercel

vercel Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
examples-todomvc Ready Ready Preview Nov 27, 2026 1:47pm UTC
next-prisma-starter Ready Ready Preview Nov 27, 2026 1:47pm UTC
trpc-next-app-dir Ready Ready Preview Nov 27, 2026 1:47pm UTC
www Ready Ready Preview Nov 27, 2026 1:47pm UTC

Request Review

@vercel

vercel Bot commented Nov 27, 2026

Copy link
Copy Markdown

@sagarrsharmaa is attempting to deploy a commit to the trpc Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

When a response contains cookies, writeResponse now appends them to existing set-cookie header values. Regression tests cover the standalone and Express adapters, including cookies set by middleware, procedures, and responseMeta.

Changes: Cookie header handling

Layer / File(s) Summary
Cookie combination and adapter coverage
packages/server/src/adapters/node-http/writeResponse.ts, packages/tests/server/regression/nodeSetCookieHeaders.test.ts
writeResponse combines existing set-cookie values with response cookies and converts the values to strings. Regression tests verify the cookie order and 200 status for the standalone and Express adapters. The Express test also checks Path=/ on middleware and procedure cookies.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: nick-lucas, katt

Merge Risk: Minimal · up to 96008

This change keeps cookies already set on the response when responseMeta also sets a cookie, for the standalone and Express adapters. It has regression tests for both and no identified merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix(server): keep Set-Cookie headers set before responseMeta" matches the main change. The writeResponse.ts change now keeps cookies already set on the raw response and appends responseMeta…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "fix(server): keep Set-Cookie headers set before responseMeta" matches the main change. The writeResponse.ts change now keeps cookies already set on the raw response and appends responseMeta cookies, instead of replacing the existing set-cookie header. The added regression tests cover standalone and Express adapters, which matches the objectives.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: trpc/trpc
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: ee3e8f2
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a22e9763-6cde-46ed-9bfd-3cf293a4be39
Commits

Reviewing files that changed from the base of the PR and between ee3e8f2 and 960086e.

Files selected for processing: 2

  • packages/server/src/adapters/node-http/writeResponse.ts
  • packages/tests/server/regression/nodeSetCookieHeaders.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@trpc-bot trpc-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cookies set on res before responseMeta now survive, in order, as they already do in the fetch adapter. The test fails on main.

@trpc-bot
trpc-bot added this pull request to the merge queue Nov 27, 2026
Merged via the queue into trpc:main with commit 4441b92 Nov 27, 2026
32 of 36 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

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.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 28, 2026

This branch was successfully deployed

4 active deployments
Preview – www — 960086e6 Deployed Nov 27, 2026 by vercel[bot]
Preview – trpc-next-app-dir — 960086e6 Deployed Nov 27, 2026 by vercel[bot]
Preview – next-prisma-starter — 960086e6 Deployed Nov 27, 2026 by vercel[bot]
Preview – examples-todomvc — 960086e6 Deployed Nov 27, 2026 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants