fix: match bare basePath root for catch-all middleware matchers#94304
Open
Xavrir wants to merge 1 commit into
Open
fix: match bare basePath root for catch-all middleware matchers#94304Xavrir wants to merge 1 commit into
Xavrir wants to merge 1 commit into
Conversation
| // without a `basePath`. Restore that by also accepting the bare | ||
| // `basePath` root. See https://github.com/vercel/next.js/issues/73786. | ||
| if (matchesRoot) { | ||
| const basePathRoot = `${escapeStringRegexp(nextConfig.basePath!)}[\\/#\\?]?$` |
Contributor
When `basePath` is set, `getMiddlewareMatchers` prepends it to each matcher
source. For a catch-all matcher like `/((?!api).*)` that previously matched the
site root `/`, the prepended form becomes anchored at `${basePath}/...`, so the
bare `basePath` root (e.g. `/docs`) no longer matches even though `/` did
without a basePath. Middleware therefore did not run on the basePath root.
Detect when a matcher matches `/` before prepending basePath, and in that case
also accept the bare basePath root by OR-ing in an anchored
`^${basePath}[/#?]?$` alternative. Specific and param matchers are unaffected.
Adds regression tests covering catch-all, static, and param matchers.
Closes vercel#73786
0d986f7 to
d3dec31
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
When
basePathis set, middleware did not run on the bare basePath root. Withand
basePath: '/docs'innext.config, a request to/docsskippedmiddleware, while
/docs/and/docs/fooran it. Without a basePath the samematcher runs on
/. Fixes #73786.Why
getMiddlewareMatchersinpackages/next/src/build/analysis/get-page-static-info.tsbuilds each matcher's source and then prepends
basePath. A catch-all like/((?!api).*)matches/because its one capture group can be empty, so thecompiled regex is
^(?:/(...))(...)?[/#?]?$and/matches.Once
basePathis prepended the regex becomes^/docs(?:/(...))..., whichrequires a slash right after
/docs. The bare/docsthen fails to match eventhough
/matched before. The matcher is baked into the middleware manifest, sothis affects both the dev server and production (including Vercel), not just one
runtime.
How
Before prepending
basePath, check whether the matcher regex (without basePath)matches
/. If it does, after building the final regex also accept the barebasePath root by OR-ing in an anchored alternative:
The check is gated on
basePathbeing set and the matcher not being the literal/root (which already has its own handling). Specific matchers like/dashboardand param matchers like/:iddo not match/, so they are leftunchanged and do not start matching the basePath root.
Tests
Extended
get-page-static-info.test.tswith three cases:basePath: '/docs':/docs,/docs/,/docs/foomatch;
/docs/api/x,/docsfoo,/foodo not./dashboard:/docs/dashboardmatches;/docsand/docs/do not./:id:/docs/applematches;/docsdoes not.All nine tests in the file pass (six existing plus the three new ones):
prettier and eslint were run on both changed files.
Signed commits
The commit is signed (SSH) and shows as verified, so it meets the protected
branch requirement.