Skip to content

fix(server): keep the Vary header a middleware set - #8216

Merged
trpc-bot merged 1 commit into
trpc:mainfrom
sagarrsharmaa:fix/keep-cors-vary-header
Nov 27, 2026
Merged

trpc-bot merged 1 commit into
trpc:mainfrom
sagarrsharmaa:fix/keep-cors-vary-header

Conversation

@sagarrsharmaa

@sagarrsharmaa sagarrsharmaa commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Why

  • cors() and @fastify/cors add Vary: Origin so a cache keeps one response per origin. The Node adapters (standalone, Express, Next.js API routes) and the Fastify adapter replace it with tRPC's Vary: trpc-accept, accept.
  • A CDN caching a query, as the response caching docs suggest, can then serve one origin's Access-Control-Allow-Origin to another origin, and the browser blocks the call.

What

  • The Node adapters add tRPC's Vary fields to the ones already on res, without repeating a field.
  • The Fastify adapter does the same with the reply's Vary before sending the response.
  • If nothing set Vary before tRPC, the header is unchanged.

Links

None

Alternatives considered

  • Asking users to add Origin again in responseMeta: they'd have to know tRPC drops it, and nothing in their CORS setup hints at it.

Testing

  • Regression check: packages/tests/server/regression/corsVaryHeader.test.ts sets Vary: Origin in a standalone middleware and in a Fastify onRequest hook. On main both responses come back with only Vary: trpc-accept, accept.

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
    • Responses now preserve existing Vary header values when adding framework-specific values. Duplicate entries are removed, while the original ordering and spelling are retained.
    • This improves handling of CORS responses that vary by request origin, helping ensure caches distinguish responses intended for different origins. The behavior is consistent across the Node.js and Fastify server adapters.

`cors()` with a list of allowed origins, and `@fastify/cors`, echo the
request's origin in `Access-Control-Allow-Origin` and add `Vary: Origin`
so that a cache keeps one response per origin. The Node.js adapters
(standalone, Express, Next.js API routes) and the Fastify adapter then
replaced that header with tRPC's own `Vary: trpc-accept, accept`. A CDN
caching a query, as the response caching docs suggest, could then serve
one origin's CORS headers to another origin, and the browser blocks it.

tRPC's `Vary` fields are now added to the ones already on the response,
without repeating a field.
@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:46pm UTC
next-prisma-starter Ready Ready Preview Nov 27, 2026 1:46pm UTC
trpc-next-app-dir Ready Ready Preview Nov 27, 2026 1:46pm UTC
www Ready Ready Preview Nov 27, 2026 1:46pm 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

The Node HTTP response utility now merges Vary values, and both the standalone and Fastify adapters preserve existing values when sending responses. Regression tests check the merged header in both adapters.

Changes: Vary Header Handling

Layer / File(s) Summary
Merge Vary values across adapters
packages/server/src/adapters/node-http/writeResponse.ts, packages/server/src/adapters/fastify/fastifyRequestHandler.ts, packages/tests/server/regression/corsVaryHeader.test.ts
The Node HTTP utility trims comma-separated Vary values, drops empty fields, and deduplicates names without regard to case. The Node HTTP and Fastify adapters use the merged values. Regression tests check the response status, echoed origin, and exact Vary header for both adapters.

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

Change: Bug fix

Suggested reviewers: katt, nick-lucas

Merge Risk: Minimal · up to 43cbd

This change keeps a Vary header set by middleware, such as Vary: Origin from CORS, alongside tRPC's own fields. That helps prevent CDNs from serving one origin's CORS response to another. Regression tests cover the standalone and Fastify adapters, and no merge-blocking risk was identified.

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 the Vary header a middleware set" matches the main change. The PR makes tRPC's Node and Fastify adapters merge tRPC's Vary fields (trpc-accept, accept) into any existing V…
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 the Vary header a middleware set" matches the main change. The PR makes tRPC's Node and Fastify adapters merge tRPC's Vary fields (trpc-accept, accept) into any existing Vary value set by middleware, instead of replacing it. The title names the server scope, the Vary header, and the middleware case, so it is specific and concise. It does not list every change, such as the new mergeVary helper or the regression tests, which is acceptable.

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: 6de3973b-1761-4139-9fb8-ea97a553bb40
Commits

Reviewing files that changed from the base of the PR and between ee3e8f2 and 43cbd4c.

Files selected for processing: 3

  • packages/server/src/adapters/fastify/fastifyRequestHandler.ts
  • packages/server/src/adapters/node-http/writeResponse.ts
  • packages/tests/server/regression/corsVaryHeader.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 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.

Fixes it, and the tests fail on main. Two nits for a follow-up, not blocking: in mergeVary, return * when any field is * (*, trpc-accept, accept is harmless but not well formed); and Headers already gives lowercase keys, so the .toLowerCase() on the key in writeResponse.ts isn't needed.

@trpc-bot
trpc-bot added this pull request to the merge queue Nov 27, 2026
Merged via the queue into trpc:main with commit 18333d1 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 — 43cbd4c3 Deployed Nov 27, 2026 by vercel[bot]
Preview – trpc-next-app-dir — 43cbd4c3 Deployed Nov 27, 2026 by vercel[bot]
Preview – next-prisma-starter — 43cbd4c3 Deployed Nov 27, 2026 by vercel[bot]
Preview – examples-todomvc — 43cbd4c3 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