Skip to content

Commit 43fd03c

Browse files
committed
fix: narrow down middleware i18n locale matcher to concrete locales
1 parent e1b3b8a commit 43fd03c

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/build/functions/edge.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,23 @@ const augmentMatchers = (
3636
matchers: NextDefinition['matchers'],
3737
ctx: PluginContext,
3838
): NextDefinition['matchers'] => {
39-
if (!ctx.buildConfig.i18n) {
39+
const i18NConfig = ctx.buildConfig.i18n
40+
if (!i18NConfig) {
4041
return matchers
4142
}
4243
return matchers.flatMap((matcher) => {
4344
if (matcher.originalSource && matcher.locale !== false) {
4445
return [
45-
matcher,
46+
matcher.regexp
47+
? {
48+
...matcher,
49+
// https://github.com/vercel/next.js/blob/5e236c9909a768dc93856fdfad53d4f4adc2db99/packages/next/src/build/analysis/get-page-static-info.ts#L332-L336
50+
// Next is producing pretty broad matcher for i18n locale. Presumably rest of their infrastructure protects this broad matcher
51+
// from matching on non-locale paths. For us this becomes request entry point, so we need to narrow it down to just defined locales
52+
// otherwise users might get unexpected matches on paths like `/api*`
53+
regexp: matcher.regexp.replace(/\[\^\/\.]+/g, `(${i18NConfig.locales.join('|')})`),
54+
}
55+
: matcher,
4656
{
4757
...matcher,
4858
regexp: pathToRegexp(matcher.originalSource).source,

0 commit comments

Comments
 (0)