Skip to content

fix: middleware matcher should match root path with basePath#92554

Open
Albearoti wants to merge 2 commits intovercel:canaryfrom
Albearoti:fix/middleware-matcher-basepath-root
Open

fix: middleware matcher should match root path with basePath#92554
Albearoti wants to merge 2 commits intovercel:canaryfrom
Albearoti:fix/middleware-matcher-basepath-root

Conversation

@Albearoti
Copy link
Copy Markdown

Summary

  • When basePath is set, middleware matchers using negative lookahead patterns like /((?!api|_next/static|...).*) failed to match the root basePath URL (e.g. /test)
  • The root check source === '/' only recognized the literal /, missing patterns that semantically match root
  • Now detects when a source pattern can match / and wraps it in an optional group {...}? so the basePath alone triggers the middleware
  • Falls back to the original behavior if the optional wrapping fails to parse (e.g. for named params like /:path*)

Test plan

  • Verified regex matches: /test (root), /test/foo, /test/about all match
  • Verified exclusions still work: /test/api, /test/_next/static, /test/favicon.ico don't match
  • Verified non-root patterns unaffected: /hello with basePath still works correctly
  • Verified no basePath case unchanged
  • Zod validation passes for the new source pattern

Fixes #73786

When basePath is set, middleware matchers using patterns like
/((?!api|_next/static|...).*)  failed to match the root basePath URL
(e.g. /test) because the pattern required content after the basePath.

Now detects when a source pattern can match "/" and makes it optional
in the compiled regex, so basePath alone triggers the middleware.

Fixes vercel#73786
@nextjs-bot
Copy link
Copy Markdown
Collaborator

nextjs-bot commented Apr 9, 2026

Allow CI Workflow Run

  • approve CI run for commit: 727a719

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

Comment thread packages/next/src/build/analysis/get-page-static-info.ts Outdated
…h basePath

When both basePath and i18n are configured and the source pattern can match
the root path, include the i18n locale prefix in the optional source pattern
to preserve locale-aware matching behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Middleware matcher does not catch root path if base path is set

2 participants