mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 08:25:34 +02:00
[PR #303] [MERGED] fix: rebuild reqCtx for rewrites after middleware #460
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#460
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/303
Author: @james-elicx
Created: 3/6/2026
Status: ✅ Merged
Merged: 3/6/2026
Merged by: @james-elicx
Base:
main← Head:james/fix-rewrite-req-ctx📝 Commits (6)
a15778afix: rebuild reqCtx for afterFiles/fallback rewrites after middlewaree922cadfix: also use postMwReqCtx for beforeFiles rewrites in deploy.ts28a0e46fix: resolve merge conflict - keep Vary header test and afterFiles rewrite test40b8455fix: move beforeFiles rewrites after middleware in app-dev-server; add has/missing regression tests6b153b1Apply suggestion from @james-elicx86561fcfix: use postMwReqCtx for beforeFiles in prod-server (Pages Router)📊 Changes
10 files changed (+241 additions, -37 deletions)
View changed files
📝
packages/vinext/src/deploy.ts(+13 -4)📝
packages/vinext/src/server/app-dev-server.ts(+46 -18)📝
packages/vinext/src/server/prod-server.ts(+12 -4)📝
packages/vinext/src/shims/headers.ts(+9 -0)📝
tests/app-router.test.ts(+44 -9)📝
tests/fixtures/app-basic/middleware.ts(+23 -0)📝
tests/fixtures/app-basic/next.config.ts(+19 -1)📝
tests/fixtures/pages-basic/middleware.ts(+21 -0)📝
tests/fixtures/pages-basic/next.config.mjs(+18 -0)📝
tests/pages-router.test.ts(+36 -1)📄 Description
All three rewrite types —
beforeFiles,afterFiles, andfallback— run after middleware per the Next.js execution order:This applies to both App Router and Pages Router (confirmed in the Next.js 14 docs).
What was wrong
has/missingconditions on rewrite rules were evaluated against the pre-middleware request context, so middleware-injected cookies/headers were invisible to the matcher.Additionally,
beforeFilesrewrites were running before middleware inapp-dev-server.ts— a pre-existing ordering bug, now fixed.What changed
app-dev-server.ts: MovedbeforeFilesblock to after middleware execution. Added__buildPostMwRequestContext()helper that reads the ALS-stored headers context (post-middleware) and convertsMap<string,string>→Record<string,string>for cookie lookups. All three rewrite types now use this post-middleware context.prod-server.ts:beforeFilesnow usespostMwReqCtx(was incorrectly using the pre-middleware snapshot).headersandredirectscorrectly retain the pre-middleware context.deploy.ts: Already correct forafterFiles/fallback;beforeFilesalso switched topostMwReqCtx.shims/headers.ts: ExportedgetHeadersContext()for use in the post-middleware context helper.Tests added
Regression tests covering all three rewrite types across both routers:
afterFilesprod-server.ts)beforeFilesapp-dev-server.ts)beforeFilesprod-server.ts)fallbackapp-dev-server.ts)Each test verifies: without
?mw-auththe rule does not match (404); with?mw-authmiddleware injects the cookie and the rule matches (200, rewrites to/about).🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.