[PR #303] [MERGED] fix: rebuild reqCtx for rewrites after middleware #460

Closed
opened 2026-05-06 12:39:54 +02:00 by BreizhHardware · 0 comments

📋 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: mainHead: james/fix-rewrite-req-ctx


📝 Commits (6)

  • a15778a fix: rebuild reqCtx for afterFiles/fallback rewrites after middleware
  • e922cad fix: also use postMwReqCtx for beforeFiles rewrites in deploy.ts
  • 28a0e46 fix: resolve merge conflict - keep Vary header test and afterFiles rewrite test
  • 40b8455 fix: move beforeFiles rewrites after middleware in app-dev-server; add has/missing regression tests
  • 6b153b1 Apply suggestion from @james-elicx
  • 86561fc fix: 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, and fallback — run after middleware per the Next.js execution order:

headers → redirects → Middleware → beforeFiles → filesystem → afterFiles → fallback

This applies to both App Router and Pages Router (confirmed in the Next.js 14 docs).

What was wrong

has/missing conditions on rewrite rules were evaluated against the pre-middleware request context, so middleware-injected cookies/headers were invisible to the matcher.

Additionally, beforeFiles rewrites were running before middleware in app-dev-server.ts — a pre-existing ordering bug, now fixed.

What changed

  • app-dev-server.ts: Moved beforeFiles block to after middleware execution. Added __buildPostMwRequestContext() helper that reads the ALS-stored headers context (post-middleware) and converts Map<string,string>Record<string,string> for cookie lookups. All three rewrite types now use this post-middleware context.
  • prod-server.ts: beforeFiles now uses postMwReqCtx (was incorrectly using the pre-middleware snapshot). headers and redirects correctly retain the pre-middleware context.
  • deploy.ts: Already correct for afterFiles/fallback; beforeFiles also switched to postMwReqCtx.
  • shims/headers.ts: Exported getHeadersContext() for use in the post-middleware context helper.

Tests added

Regression tests covering all three rewrite types across both routers:

Rewrite type Router Server under test
afterFiles Pages Router dev + prod (prod-server.ts)
beforeFiles App Router dev (app-dev-server.ts)
beforeFiles Pages Router prod (prod-server.ts)
fallback App Router dev (app-dev-server.ts)

Each test verifies: without ?mw-auth the rule does not match (404); with ?mw-auth middleware 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.

## 📋 Pull Request Information **Original PR:** https://github.com/cloudflare/vinext/pull/303 **Author:** [@james-elicx](https://github.com/james-elicx) **Created:** 3/6/2026 **Status:** ✅ Merged **Merged:** 3/6/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `james/fix-rewrite-req-ctx` --- ### 📝 Commits (6) - [`a15778a`](https://github.com/cloudflare/vinext/commit/a15778a6239dbf0f60a10a40a5ecd7f851f48877) fix: rebuild reqCtx for afterFiles/fallback rewrites after middleware - [`e922cad`](https://github.com/cloudflare/vinext/commit/e922cadfa8e6522e9254e1ab94dd22f50c97e82f) fix: also use postMwReqCtx for beforeFiles rewrites in deploy.ts - [`28a0e46`](https://github.com/cloudflare/vinext/commit/28a0e46bee0c37a40d7de0c803b316a4c5e6be71) fix: resolve merge conflict - keep Vary header test and afterFiles rewrite test - [`40b8455`](https://github.com/cloudflare/vinext/commit/40b8455d25f14551560d2e663c6cdd7d4154f768) fix: move beforeFiles rewrites after middleware in app-dev-server; add has/missing regression tests - [`6b153b1`](https://github.com/cloudflare/vinext/commit/6b153b1d1364a17386f02bfea6a05a5028fb86b2) Apply suggestion from @james-elicx - [`86561fc`](https://github.com/cloudflare/vinext/commit/86561fc530e7d64c3069e2c1124c35c57d8444f1) fix: use postMwReqCtx for beforeFiles in prod-server (Pages Router) ### 📊 Changes **10 files changed** (+241 additions, -37 deletions) <details> <summary>View changed files</summary> 📝 `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) </details> ### 📄 Description All three rewrite types — `beforeFiles`, `afterFiles`, and `fallback` — run after middleware per the Next.js execution order: ``` headers → redirects → Middleware → beforeFiles → filesystem → afterFiles → fallback ``` This applies to both App Router and Pages Router (confirmed in the [Next.js 14 docs](https://nextjs.org/docs/14/pages/building-your-application/routing/middleware#matching-paths)). ## What was wrong `has`/`missing` conditions on rewrite rules were evaluated against the pre-middleware request context, so middleware-injected cookies/headers were invisible to the matcher. Additionally, `beforeFiles` rewrites were running *before* middleware in `app-dev-server.ts` — a pre-existing ordering bug, now fixed. ## What changed - **`app-dev-server.ts`**: Moved `beforeFiles` block to after middleware execution. Added `__buildPostMwRequestContext()` helper that reads the ALS-stored headers context (post-middleware) and converts `Map<string,string>` → `Record<string,string>` for cookie lookups. All three rewrite types now use this post-middleware context. - **`prod-server.ts`**: `beforeFiles` now uses `postMwReqCtx` (was incorrectly using the pre-middleware snapshot). `headers` and `redirects` correctly retain the pre-middleware context. - **`deploy.ts`**: Already correct for `afterFiles`/`fallback`; `beforeFiles` also switched to `postMwReqCtx`. - **`shims/headers.ts`**: Exported `getHeadersContext()` for use in the post-middleware context helper. ## Tests added Regression tests covering all three rewrite types across both routers: | Rewrite type | Router | Server under test | |---|---|---| | `afterFiles` | Pages Router | dev + prod (`prod-server.ts`) | | `beforeFiles` | App Router | dev (`app-dev-server.ts`) | | `beforeFiles` | Pages Router | prod (`prod-server.ts`) | | `fallback` | App Router | dev (`app-dev-server.ts`) | Each test verifies: without `?mw-auth` the rule does not match (404); with `?mw-auth` middleware injects the cookie and the rule matches (200, rewrites to `/about`). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 12:39:54 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#460
No description provided.