mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 08:25:34 +02:00
[PR #388] [MERGED] perf: lazy-init mutable Headers copy in headersContextFromRequest #531
Labels
No labels
enhancement
enhancement
good first issue
help wanted
nextjs-tracking
nextjs-tracking
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/vinext#531
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/cloudflare/vinext/pull/388
Author: @james-elicx
Created: 3/9/2026
Status: ✅ Merged
Merged: 3/9/2026
Merged by: @james-elicx
Base:
main← Head:perf/lazy-headers-context📝 Commits (2)
8182de5perf: lazy-init mutable Headers copy in headersContextFromRequest6f2c766perf: hoist MUTATING_METHODS set to module scope📊 Changes
2 files changed (+146 additions, -13 deletions)
View changed files
📝
packages/vinext/src/shims/headers.ts(+82 -13)📝
tests/shims.test.ts(+64 -0)📄 Description
Problem
headersContextFromRequestwas profiled at 815ms self-time per request with no children — all time spent in its own body. The two culprits:new Headers(request.headers)— in Workerd, this copies the entire header map across the V8/C++ boundary. On requests with many headers (e.g. a browser sending lots of cookies), this is very slow.cookieheader on;and=on every request, even whencookies()is never called.Both operations happened unconditionally on every request entry, even for routes that never read cookies or mutate headers.
Fix
Lazy mutable Headers proxy
Replace
new Headers(request.headers)with aProxyaround the original immutablerequest.headers:get,has,entries,forEach, …) are forwarded directly torequest.headers— zero copy cost.set,delete, orappend) materialisesnew Headers(request.headers)once and caches it. All subsequent operations use the copy.The copy is only needed when middleware calls
NextResponse.next({ request: { headers } })andapplyMiddlewareRequestHeaders()sets headers on the context. That path is uncommon. Pure read requests (the vast majority) now pay zero V8/C++ boundary crossing for headers.Lazy cookie parsing
Cookie parsing is moved behind a lazy getter on the
cookiesproperty. Thecookieheader string is not split untilcookies(),draftMode(), orapplyMiddlewareRequestHeaders()is first invoked for a request. Requests that don't touch cookies skip the parse entirely.Tests
Four new test cases added to
tests/shims.test.ts:defers new Headers() copy until first write— regression guard: reads must work before any writedefers cookie parsing until first access— cookies map is not populated until accessedcookie getter reflects middleware-modified cookie header— verifiesapplyMiddlewareRequestHeaderscorrectly invalidates and rebuilds the cookie map after middleware changes thecookieheaderBehavioural compatibility
The
HeadersContextinterface is unchanged.setHeadersContext({ headers, cookies })(used by tests and legacy callers) continues to accept plainHeaders/Mapobjects without change.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.