[PR #448] [MERGED] fix: Pages prod config headers and redirects after middleware rewrites #577

Closed
opened 2026-05-06 13:08:51 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/cloudflare/vinext/pull/448
Author: @JaredStowell
Created: 3/11/2026
Status: Merged
Merged: 3/11/2026
Merged by: @james-elicx

Base: mainHead: jstowell/fix-pages-prod-config-matchers-before-middleware-rewrite


📝 Commits (8)

  • 772c166 Fix headers matching original path
  • 07aba4f Review redirect ordering parity
  • be11d4e Fix config redirects order
  • 52387fe Fix dev middleware parity
  • 239ba26 Fix dev middleware request parity
  • 25d4b5b Merge remote-tracking branch 'origin/main' into jstowell/fix-pages-prod-config-matchers-before-middleware-rewrite
  • 763c6e0 address review: worker header/fallback parity, early-return in attachRouteHandlerMiddlewareContext, cache node request headers, add dev redirect test
  • 2be7a82 address second-round bonk comments: sanitizeDestination in worker redirect, nodeRequestHeaders comment, app-rsc-entry guard comment, E2E redirect test, updated snapshots

📊 Changes

13 files changed (+584 additions, -234 deletions)

View changed files

📝 examples/pages-router-cloudflare/middleware.ts (+19 -1)
examples/pages-router-cloudflare/next.config.mjs (+25 -0)
📝 examples/pages-router-cloudflare/worker/index.ts (+24 -21)
📝 packages/vinext/src/deploy.ts (+30 -27)
📝 packages/vinext/src/entries/app-rsc-entry.ts (+38 -15)
📝 packages/vinext/src/index.ts (+46 -50)
📝 packages/vinext/src/server/prod-server.ts (+31 -27)
📝 tests/__snapshots__/entry-templates.test.ts.snap (+229 -91)
📝 tests/deploy.test.ts (+11 -2)
📝 tests/e2e/cloudflare-pages-router-dev/pages-router.spec.ts (+27 -0)
📝 tests/fixtures/pages-basic/middleware.ts (+12 -0)
📝 tests/fixtures/pages-basic/next.config.mjs (+14 -0)
📝 tests/pages-router.test.ts (+78 -0)

📄 Description

Fix Pages Router production ordering so next.config.js headers() and redirects() still match against the original request pathname even when middleware rewrites the downstream render target.

Previously, Pages prod wrote the middleware rewrite into resolvedUrl, derived resolvedPathname from that rewritten URL, and then fed that rewritten pathname into matchHeaders() and matchRedirect(). That caused config headers/redirects to run against the middleware target instead of the pre-middleware request path.

This change updates both Pages production paths to preserve the original normalized pathname for config header/redirect matching while continuing to use resolvedUrl/resolvedPathname for downstream rewrites, API routing, and page rendering.

Changes

  • Update Pages prod server to use pre-middleware pathname for:
    • matchHeaders()
    • matchRedirect()
  • Mirror the same fix in the Cloudflare worker/deploy template
  • Add Pages Router regression coverage for:
    • config headers() still applying after a middleware rewrite
    • config redirects() still winning before a middleware rewrite target
  • Update worker entry generator tests and snapshots

Testing

  • pnpm test tests/pages-router.test.ts tests/deploy.test.ts tests/entry-templates.test.ts
  • pnpm run fmt
  • pnpm run typecheck

🔄 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/448 **Author:** [@JaredStowell](https://github.com/JaredStowell) **Created:** 3/11/2026 **Status:** ✅ Merged **Merged:** 3/11/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `jstowell/fix-pages-prod-config-matchers-before-middleware-rewrite` --- ### 📝 Commits (8) - [`772c166`](https://github.com/cloudflare/vinext/commit/772c166370dcc72d65f9b291f0bea2285ec77443) Fix headers matching original path - [`07aba4f`](https://github.com/cloudflare/vinext/commit/07aba4f8cc8229d329f64b70a15fe65be1d036ca) Review redirect ordering parity - [`be11d4e`](https://github.com/cloudflare/vinext/commit/be11d4eb5f50c9adede2a5d4f61a0c6ebe5b847f) Fix config redirects order - [`52387fe`](https://github.com/cloudflare/vinext/commit/52387fea98456f3422c3a36ebbedd7cf8a4d31c7) Fix dev middleware parity - [`239ba26`](https://github.com/cloudflare/vinext/commit/239ba269ad23dd3229ec9d429743054ae8ae6d13) Fix dev middleware request parity - [`25d4b5b`](https://github.com/cloudflare/vinext/commit/25d4b5b4f6a8ba3a3dce655170e42420670637a2) Merge remote-tracking branch 'origin/main' into jstowell/fix-pages-prod-config-matchers-before-middleware-rewrite - [`763c6e0`](https://github.com/cloudflare/vinext/commit/763c6e0718674df89165199803171f593f0bcb82) address review: worker header/fallback parity, early-return in attachRouteHandlerMiddlewareContext, cache node request headers, add dev redirect test - [`2be7a82`](https://github.com/cloudflare/vinext/commit/2be7a82daeb9ebeaa047621b005fb01522c36ab6) address second-round bonk comments: sanitizeDestination in worker redirect, nodeRequestHeaders comment, app-rsc-entry guard comment, E2E redirect test, updated snapshots ### 📊 Changes **13 files changed** (+584 additions, -234 deletions) <details> <summary>View changed files</summary> 📝 `examples/pages-router-cloudflare/middleware.ts` (+19 -1) ➕ `examples/pages-router-cloudflare/next.config.mjs` (+25 -0) 📝 `examples/pages-router-cloudflare/worker/index.ts` (+24 -21) 📝 `packages/vinext/src/deploy.ts` (+30 -27) 📝 `packages/vinext/src/entries/app-rsc-entry.ts` (+38 -15) 📝 `packages/vinext/src/index.ts` (+46 -50) 📝 `packages/vinext/src/server/prod-server.ts` (+31 -27) 📝 `tests/__snapshots__/entry-templates.test.ts.snap` (+229 -91) 📝 `tests/deploy.test.ts` (+11 -2) 📝 `tests/e2e/cloudflare-pages-router-dev/pages-router.spec.ts` (+27 -0) 📝 `tests/fixtures/pages-basic/middleware.ts` (+12 -0) 📝 `tests/fixtures/pages-basic/next.config.mjs` (+14 -0) 📝 `tests/pages-router.test.ts` (+78 -0) </details> ### 📄 Description Fix Pages Router production ordering so `next.config.js` `headers()` and `redirects()` still match against the original request pathname even when middleware rewrites the downstream render target. Previously, Pages prod wrote the middleware rewrite into `resolvedUrl`, derived `resolvedPathname` from that rewritten URL, and then fed that rewritten pathname into `matchHeaders()` and `matchRedirect()`. That caused config headers/redirects to run against the middleware target instead of the pre-middleware request path. This change updates both Pages production paths to preserve the original normalized `pathname` for config header/redirect matching while continuing to use `resolvedUrl`/`resolvedPathname` for downstream rewrites, API routing, and page rendering. ## Changes - Update Pages prod server to use pre-middleware `pathname` for: - `matchHeaders()` - `matchRedirect()` - Mirror the same fix in the Cloudflare worker/deploy template - Add Pages Router regression coverage for: - config `headers()` still applying after a middleware rewrite - config `redirects()` still winning before a middleware rewrite target - Update worker entry generator tests and snapshots ## Testing - `pnpm test tests/pages-router.test.ts tests/deploy.test.ts tests/entry-templates.test.ts` - `pnpm run fmt` - `pnpm run typecheck` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 13:08:51 +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#577
No description provided.