Repository navigation
fix(server): keep the Vary header a middleware set - #8216
Conversation
`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.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
@sagarrsharmaa is attempting to deploy a commit to the trpc Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
trpc-bot
left a comment
There was a problem hiding this comment.
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.
|
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
cors()and@fastify/corsaddVary: Originso a cache keeps one response per origin. The Node adapters (standalone, Express, Next.js API routes) and the Fastify adapter replace it with tRPC'sVary: trpc-accept, accept.Access-Control-Allow-Originto another origin, and the browser blocks the call.What
Varyfields to the ones already onres, without repeating a field.Varybefore sending the response.Varybefore tRPC, the header is unchanged.Links
None
Alternatives considered
Originagain inresponseMeta: they'd have to know tRPC drops it, and nothing in their CORS setup hints at it.Testing
packages/tests/server/regression/corsVaryHeader.test.tssetsVary: Originin a standalonemiddlewareand in a FastifyonRequesthook. Onmainboth responses come back with onlyVary: trpc-accept, accept.AI model
Opus 5.5
Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.Summary by CodeRabbit
Varyheader values when adding framework-specific values. Duplicate entries are removed, while the original ordering and spelling are retained.