mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 08:25:34 +02:00
[PR #541] [MERGED] fix: eliminate double middleware execution in hybrid app+pages dev mode #656
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#656
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/541
Author: @NathanDrake2406
Created: 3/15/2026
Status: ✅ Merged
Merged: 3/16/2026
Merged by: @james-elicx
Base:
main← Head:fix/double-middleware-execution📝 Commits (5)
77d6620fix: eliminate double middleware execution in hybrid app+pages dev mode51c8fbetest: update entry template snapshot for middleware forwardingadb293bfix: address code review findings for middleware forwarding7662eb9test: update entry template snapshot for review fixes7c18acefix: extract deferred headers helper, clarify flag placement per review📊 Changes
5 files changed (+152 additions, -32 deletions)
View changed files
📝
packages/vinext/src/entries/app-rsc-entry.ts(+42 -0)📝
packages/vinext/src/index.ts(+55 -5)📝
tests/__snapshots__/entry-templates.test.ts.snap(+42 -0)📝
tests/e2e/app-router/middleware.spec.ts(+5 -17)📝
tests/fixtures/app-basic/instrumentation-state.ts(+8 -10)📄 Description
Summary
x-vinext-mw-ctxrequest header. The RSC entry reconstructs_mwCtxfrom the forwarded data instead of re-executing the middleware function.pages/) and pure Pages Router (noapp/) setups are unaffected — they already use a single middleware execution path.Key design decisions
_mwCtx.headersforapplyMiddlewareRequestHeaders()(soheaders()in Server Components sees middleware-modified headers). Simply skipping middleware would break this.req.url, so middleware rewrites must be forwarded explicitly in the header payload.hasAppDiris true, middleware response headers are deferred (not applied tores) to avoid duplicates. They're applied only for Pages routes handled directly by the connect handler.x-vinext-mw-ctxcheck is guarded byNODE_ENV !== "production"since the forwarding mechanism is dev-only. In production there is no connect handler, so an attacker-supplied header must not be trusted.Test plan
test.fixmeto live regression test — verifiesmiddlewareInvocationCount === 1for App Router requests in hybrid fixture🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.