Unverified Commit 15a216b8 authored by Damien Simonin Feugas's avatar Damien Simonin Feugas Committed by GitHub

feat(middleware): augments / matcher with /index (#39397)

* feat(middleware): augments / matcher with /index

* fix(#39396): not invoked with i18n and trailingSlash

* add test case

* tweak matcher a bit and add tests
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent e2119c06
......@@ -4,7 +4,6 @@ import {
extractExportedConstValue,
UnsupportedValueError,
} from './extract-const-value'
import { escapeStringRegexp } from '../../shared/lib/escape-regexp'
import { parseModule } from './parse-module'
import { promises as fs } from 'fs'
import { tryToParsePath } from '../../lib/try-to-parse-path'
......@@ -185,6 +184,7 @@ function getMiddlewareRegExpStrings(
getMiddlewareRegExpStrings(matcher, nextConfig)
)
}
const { i18n } = nextConfig
if (typeof matcherOrMatchers !== 'string') {
throw new Error(
......@@ -197,20 +197,23 @@ function getMiddlewareRegExpStrings(
if (!matcher.startsWith('/')) {
throw new Error('`matcher`: path matcher must start with /')
}
const isRoot = matcher === '/'
if (nextConfig.i18n?.locales) {
matcher = `/:nextInternalLocale(${nextConfig.i18n.locales
.map((locale) => escapeStringRegexp(locale))
.join('|')})${
matcher === '/' && !nextConfig.trailingSlash ? '' : matcher
}`
if (i18n?.locales) {
matcher = `/:nextInternalLocale([^/.]{1,})${isRoot ? '' : matcher}`
}
matcher = `/:nextData(_next/data/[^/]{1,})?${matcher}${
isRoot
? `(${nextConfig.i18n ? '|\\.json|' : ''}/?index|/?index\\.json)?`
: '(.json)?'
}`
if (nextConfig.basePath) {
matcher = `${nextConfig.basePath}${matcher === '/' ? '' : matcher}`
matcher = `${nextConfig.basePath}${matcher}`
}
const parsedPage = tryToParsePath(matcher)
if (parsedPage.error) {
throw new Error(`Invalid path matcher: ${matcher}`)
}
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment