[PR #400] [MERGED] fix: middleware object matcher semantics and i18n parity #541

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

📋 Pull Request Information

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

Base: mainHead: jstowell/fix-middleware-matcher-options


📝 Commits (9)

  • f7c0d7c Fix middleware matcher request usage
  • 11dd9f1 Handle object middleware matchers
  • 15e137e Fix middleware object matcher
  • 3cbf1bd make ci happy
  • da8f1a7 Fix default locale middleware match
  • dae71d7 Fix default-locale matcher logic
  • 6c83c05 Restore regexp matcher support
  • 5777ea8 Fix middleware regexp matcher
  • 0186220 Merge remote-tracking branch 'upstream/main' into jstowell/fix-middleware-matcher-options

📊 Changes

14 files changed (+1019 additions, -98 deletions)

View changed files

📝 packages/vinext/src/config/config-matchers.ts (+6 -1)
📝 packages/vinext/src/entries/app-rsc-entry.ts (+13 -4)
📝 packages/vinext/src/entries/pages-server-entry.ts (+1 -1)
📝 packages/vinext/src/index.ts (+2 -0)
📝 packages/vinext/src/server/middleware-codegen.ts (+165 -13)
📝 packages/vinext/src/server/middleware.ts (+114 -16)
📝 tests/__snapshots__/entry-templates.test.ts.snap (+383 -62)
📝 tests/app-router.test.ts (+22 -0)
tests/fixtures/app-basic/app/mw-object-gated/page.tsx (+8 -0)
📝 tests/fixtures/app-basic/middleware.ts (+5 -0)
📝 tests/fixtures/pages-basic/middleware.ts (+8 -1)
tests/fixtures/pages-basic/pages/mw-object-gated.tsx (+8 -0)
📝 tests/pages-router.test.ts (+42 -0)
📝 tests/shims.test.ts (+242 -0)

📄 Description

This fixes several middleware matcher correctness issues so vinext stays aligned with Next.js behavior in both shared runtime code and generated App Router code.

What changed:

  • Fix object-form middleware matchers so has and missing are actually enforced instead of matching on source alone.
  • Support single object matcher configs, not just arrays.
  • Keep generated App Router matcher logic in sync with the shared runtime.
  • Add support for object matcher regexp and preserve locale: false behavior.
  • Fix i18n matcher semantics so middleware does not over-match bare default-locale paths.
  • Align host condition handling between codegen and shared runtime by using the same nullish fallback behavior.

Why this matters:

  • Object matchers could previously fail open and run middleware when header, cookie, query, or host conditions were not satisfied.
  • Single object matcher configs could incorrectly never match.
  • i18n-enabled apps could run middleware on URLs that Next.js would not match, especially for default-locale paths.
  • Runtime and generated matcher paths could disagree, which made behavior environment-dependent.

Tests:

  • Added and updated unit coverage for object matcher has/missing, single-object configs, regexp, host handling, and i18n behavior.
  • Added fixture-backed integration coverage for object-form matcher gating in both Pages Router and App Router.
  • Refreshed generated entry snapshots.

Validation:

  • pnpm test tests/shims.test.ts tests/entry-templates.test.ts
  • pnpm test tests/pages-router.test.ts
  • pnpm test tests/app-router.test.ts
  • 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/400 **Author:** [@JaredStowell](https://github.com/JaredStowell) **Created:** 3/10/2026 **Status:** ✅ Merged **Merged:** 3/10/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `jstowell/fix-middleware-matcher-options` --- ### 📝 Commits (9) - [`f7c0d7c`](https://github.com/cloudflare/vinext/commit/f7c0d7c776b3fbca19ce7f8ce8d894ee5b7fbea8) Fix middleware matcher request usage - [`11dd9f1`](https://github.com/cloudflare/vinext/commit/11dd9f184a395ce9cdb21c1b2f3e85963f29d5a5) Handle object middleware matchers - [`15e137e`](https://github.com/cloudflare/vinext/commit/15e137ee99c64a7581af7a05458f2579a88ab3bc) Fix middleware object matcher - [`3cbf1bd`](https://github.com/cloudflare/vinext/commit/3cbf1bd0580cbc86c5b9032a30113b78dfaa0bd2) make ci happy - [`da8f1a7`](https://github.com/cloudflare/vinext/commit/da8f1a755c65b2d6652586d936d5efa1dba7e45c) Fix default locale middleware match - [`dae71d7`](https://github.com/cloudflare/vinext/commit/dae71d716d2d8e7e79299a8a7282cb95870e66a2) Fix default-locale matcher logic - [`6c83c05`](https://github.com/cloudflare/vinext/commit/6c83c052c7e30578dbc4f02641f566a139eba6cc) Restore regexp matcher support - [`5777ea8`](https://github.com/cloudflare/vinext/commit/5777ea8266032b5f7bf2febf2a53475418a8215b) Fix middleware regexp matcher - [`0186220`](https://github.com/cloudflare/vinext/commit/01862205a330f1a2dad9b0794fd5b56feb375fc1) Merge remote-tracking branch 'upstream/main' into jstowell/fix-middleware-matcher-options ### 📊 Changes **14 files changed** (+1019 additions, -98 deletions) <details> <summary>View changed files</summary> 📝 `packages/vinext/src/config/config-matchers.ts` (+6 -1) 📝 `packages/vinext/src/entries/app-rsc-entry.ts` (+13 -4) 📝 `packages/vinext/src/entries/pages-server-entry.ts` (+1 -1) 📝 `packages/vinext/src/index.ts` (+2 -0) 📝 `packages/vinext/src/server/middleware-codegen.ts` (+165 -13) 📝 `packages/vinext/src/server/middleware.ts` (+114 -16) 📝 `tests/__snapshots__/entry-templates.test.ts.snap` (+383 -62) 📝 `tests/app-router.test.ts` (+22 -0) ➕ `tests/fixtures/app-basic/app/mw-object-gated/page.tsx` (+8 -0) 📝 `tests/fixtures/app-basic/middleware.ts` (+5 -0) 📝 `tests/fixtures/pages-basic/middleware.ts` (+8 -1) ➕ `tests/fixtures/pages-basic/pages/mw-object-gated.tsx` (+8 -0) 📝 `tests/pages-router.test.ts` (+42 -0) 📝 `tests/shims.test.ts` (+242 -0) </details> ### 📄 Description This fixes several middleware matcher correctness issues so vinext stays aligned with Next.js behavior in both shared runtime code and generated App Router code. What changed: - Fix object-form middleware matchers so `has` and `missing` are actually enforced instead of matching on `source` alone. - Support single object matcher configs, not just arrays. - Keep generated App Router matcher logic in sync with the shared runtime. - Add support for object matcher `regexp` and preserve `locale: false` behavior. - Fix i18n matcher semantics so middleware does not over-match bare default-locale paths. - Align host condition handling between codegen and shared runtime by using the same nullish fallback behavior. Why this matters: - Object matchers could previously fail open and run middleware when header, cookie, query, or host conditions were not satisfied. - Single object matcher configs could incorrectly never match. - i18n-enabled apps could run middleware on URLs that Next.js would not match, especially for default-locale paths. - Runtime and generated matcher paths could disagree, which made behavior environment-dependent. Tests: - Added and updated unit coverage for object matcher `has`/`missing`, single-object configs, `regexp`, host handling, and i18n behavior. - Added fixture-backed integration coverage for object-form matcher gating in both Pages Router and App Router. - Refreshed generated entry snapshots. Validation: - `pnpm test tests/shims.test.ts tests/entry-templates.test.ts` - `pnpm test tests/pages-router.test.ts` - `pnpm test tests/app-router.test.ts` - `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:40 +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#541
No description provided.