fix(headers): expose live view of headers to api - #2912
Open
NriotHrreion wants to merge 1 commit into
Open
Conversation
commit: |
Contributor
Performance benchmarksCompared 0 improved · 0 regressed · 6 within ±1.5%
View detailed results and traces 🟢 improvement · 🔴 regression · ⚫ change below 1.5% · paired base/head |
Contributor
Author
|
It seems the failed e2e test is not directly related to the changes of this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2908. Ports vercel/next.js#97166 (vercel/next.js@3c97df5)
Overview
Next.js restored
headers()as a live, read-only view of the incoming request after a regression made it a detached snapshot. The snapshot caused ordering-dependent behavior: headers written by Proxy or middleware after the firstheaders()call were visible onrequest.headersbut not throughheaders().This PR aligns vinext with the restored nextjs semantics. Request header mutations remain observable through the same
headers()view, framework-internal headers remain unavailable to userland, andcookies()retains its snapshot behavior.What changed
function _getReadonlyHeaders(ctx: HeadersContext): Headers { if (!ctx.readonlyHeaders) { - const cleaned = new Headers(ctx.headers); - for (const header of FLIGHT_HEADERS) { - cleaned.delete(header); - } - cleaned.delete(NEXT_REQUEST_ID_HEADER); - cleaned.delete(NEXT_HTML_REQUEST_ID_HEADER); - ctx.readonlyHeaders = _sealHeaders(cleaned); + ctx.readonlyHeaders = _sealHeaders(ctx.headers, ...); } return ctx.readonlyHeaders; }_sealHeaders()and_createHidingHeaderMethods(): Wrap the original Headers, and proxy each method of it to dynamically hide the internal headers.applyMiddlewareRequestHeaders():ctx.headersto the upcoming new headers reference. So the old values still hold the old reference, not updated.replaceHeadersContents()to loop through the upcoming headers and do updates on the originalctx.headers.For cookies (
rebuildCookiesFromHeader()andmergeMiddlewareSetCookies()):ctx.cookies, merging old cookies and new cookies.ctx.cookiesto the upcoming new cookies reference. So the old values still retain the old, not-updated reference, which aligns with the upstream nextjs's behavior.headers()returns a live view of the real headers, dynamically proxied to hide the internal headers, whilecookies()still returns a readonly copy of the real cookies.Testing
pnpm test tests/shims.test.ts