Skip to content

fix: middleware not matching routes with optional parameters in fs routing#3726

Merged
bartlomieju merged 1 commit intomainfrom
fix/optional-param-segment-matching
Mar 29, 2026
Merged

fix: middleware not matching routes with optional parameters in fs routing#3726
bartlomieju merged 1 commit intomainfrom
fix/optional-param-segment-matching

Conversation

@bartlomieju
Copy link
Copy Markdown
Member

Summary

  • Fixed app.use("/api", middleware) not applying to fs routes with optional parameters like routes/api/[[opt]]/endpoint.ts
  • The root cause was patternToSegments() splitting on / characters inside {...} groups in URLPattern syntax

When routes/api/[[opt]]/endpoint.ts is converted to the pattern /api{/:opt}?/endpoint, the / inside {/:opt}? was treated as a segment boundary, producing segments ["", "api{", ":opt}?"] instead of ["", "api"]. This caused the route to land in a different segment tree node than the middleware registered at /api.

The fix strips {...}? optional groups before segmenting, so /api{/:opt}?/endpoint produces the same segments as /api/endpoint.

Closes #3545

Test plan

  • Added test cases for patternToSegments with {/:opt}? syntax
  • All 59 router/segments/app tests pass
  • All 22 builder tests pass

🤖 Generated with Claude Code

…uting

Closes #3545

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bartlomieju bartlomieju merged commit 9e7adc0 into main Mar 29, 2026
9 checks passed
@bartlomieju bartlomieju deleted the fix/optional-param-segment-matching branch March 29, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use route with path isn't matched when using file routes with an optional parameter

1 participant