[PR #495] [MERGED] fix: parse :param(constraint) in middleware matchers #616

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

📋 Pull Request Information

Original PR: https://github.com/cloudflare/vinext/pull/495
Author: @NathanDrake2406
Created: 3/12/2026
Status: Merged
Merged: 3/12/2026
Merged by: @james-elicx

Base: mainHead: fix/middleware-param-constraints


📝 Commits (2)

  • 09079c2 fix: parse :param(constraint) in middleware matchers
  • 75f22e3 fix: support constraints on catch-all params and add negative optional test

📊 Changes

4 files changed (+206 additions, -18 deletions)

View changed files

📝 packages/vinext/src/server/middleware-codegen.ts (+37 -4)
📝 packages/vinext/src/server/middleware.ts (+47 -6)
📝 tests/__snapshots__/entry-templates.test.ts.snap (+74 -8)
📝 tests/shims.test.ts (+48 -0)

📄 Description

Summary

  • Middleware matcher compiler (compileMatcherPattern) treated any pattern containing ( as a raw regex, so /blog/:id(\d+) was compiled as the literal regex ^/blog/:id(\d+)$ — matching /blog/:id123 instead of /blog/123
  • Ported the constraint-aware extractConstraint() approach from config-matchers.ts: when ( follows a :param name, extract it as an inline regex constraint and emit a proper capture group
  • Patterns without :param( syntax (e.g. /((?!api|_next).*)) still take the raw regex path
  • Also supports :param(constraint)? for optional constrained segments (e.g. /:locale(en|es|fr)?/about matches both /about and /en/about)
  • Mirrored in middleware-codegen.ts to keep runtime and generated code in sync

Test plan

  • Added matchPattern tests for :id(\d+) constraint (positive and negative)
  • Added matchPattern tests for :locale(en|es|fr) alternation constraint
  • Added matchPattern tests for :locale(en|es|fr)? optional constraint
  • Added constraint tests to both modern and es5 codegen paths
  • All existing middleware matcher tests pass (68 tests)
  • Entry template snapshots updated
  • Pages Router and routing tests pass (255 tests)
  • Typecheck, lint, format all clean
  • CI: full Vitest suite
  • CI: Playwright E2E

🔄 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/495 **Author:** [@NathanDrake2406](https://github.com/NathanDrake2406) **Created:** 3/12/2026 **Status:** ✅ Merged **Merged:** 3/12/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `fix/middleware-param-constraints` --- ### 📝 Commits (2) - [`09079c2`](https://github.com/cloudflare/vinext/commit/09079c22c4952f80da574b1919ee4b509a5d2b88) fix: parse :param(constraint) in middleware matchers - [`75f22e3`](https://github.com/cloudflare/vinext/commit/75f22e37c579565f079dea82dd1bf2982016a914) fix: support constraints on catch-all params and add negative optional test ### 📊 Changes **4 files changed** (+206 additions, -18 deletions) <details> <summary>View changed files</summary> 📝 `packages/vinext/src/server/middleware-codegen.ts` (+37 -4) 📝 `packages/vinext/src/server/middleware.ts` (+47 -6) 📝 `tests/__snapshots__/entry-templates.test.ts.snap` (+74 -8) 📝 `tests/shims.test.ts` (+48 -0) </details> ### 📄 Description ## Summary - Middleware matcher compiler (`compileMatcherPattern`) treated any pattern containing `(` as a raw regex, so `/blog/:id(\d+)` was compiled as the literal regex `^/blog/:id(\d+)$` — matching `/blog/:id123` instead of `/blog/123` - Ported the constraint-aware `extractConstraint()` approach from `config-matchers.ts`: when `(` follows a `:param` name, extract it as an inline regex constraint and emit a proper capture group - Patterns without `:param(` syntax (e.g. `/((?!api|_next).*)`) still take the raw regex path - Also supports `:param(constraint)?` for optional constrained segments (e.g. `/:locale(en|es|fr)?/about` matches both `/about` and `/en/about`) - Mirrored in `middleware-codegen.ts` to keep runtime and generated code in sync ## Test plan - [x] Added `matchPattern` tests for `:id(\d+)` constraint (positive and negative) - [x] Added `matchPattern` tests for `:locale(en|es|fr)` alternation constraint - [x] Added `matchPattern` tests for `:locale(en|es|fr)?` optional constraint - [x] Added constraint tests to both `modern` and `es5` codegen paths - [x] All existing middleware matcher tests pass (68 tests) - [x] Entry template snapshots updated - [x] Pages Router and routing tests pass (255 tests) - [x] Typecheck, lint, format all clean - [ ] CI: full Vitest suite - [ ] CI: Playwright E2E --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 13:09:06 +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#616
No description provided.