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 { ...@@ -4,7 +4,6 @@ import {
extractExportedConstValue, extractExportedConstValue,
UnsupportedValueError, UnsupportedValueError,
} from './extract-const-value' } from './extract-const-value'
import { escapeStringRegexp } from '../../shared/lib/escape-regexp'
import { parseModule } from './parse-module' import { parseModule } from './parse-module'
import { promises as fs } from 'fs' import { promises as fs } from 'fs'
import { tryToParsePath } from '../../lib/try-to-parse-path' import { tryToParsePath } from '../../lib/try-to-parse-path'
...@@ -185,6 +184,7 @@ function getMiddlewareRegExpStrings( ...@@ -185,6 +184,7 @@ function getMiddlewareRegExpStrings(
getMiddlewareRegExpStrings(matcher, nextConfig) getMiddlewareRegExpStrings(matcher, nextConfig)
) )
} }
const { i18n } = nextConfig
if (typeof matcherOrMatchers !== 'string') { if (typeof matcherOrMatchers !== 'string') {
throw new Error( throw new Error(
...@@ -197,20 +197,23 @@ function getMiddlewareRegExpStrings( ...@@ -197,20 +197,23 @@ function getMiddlewareRegExpStrings(
if (!matcher.startsWith('/')) { if (!matcher.startsWith('/')) {
throw new Error('`matcher`: path matcher must start with /') throw new Error('`matcher`: path matcher must start with /')
} }
const isRoot = matcher === '/'
if (nextConfig.i18n?.locales) { if (i18n?.locales) {
matcher = `/:nextInternalLocale(${nextConfig.i18n.locales matcher = `/:nextInternalLocale([^/.]{1,})${isRoot ? '' : matcher}`
.map((locale) => escapeStringRegexp(locale))
.join('|')})${
matcher === '/' && !nextConfig.trailingSlash ? '' : matcher
}`
} }
matcher = `/:nextData(_next/data/[^/]{1,})?${matcher}${
isRoot
? `(${nextConfig.i18n ? '|\\.json|' : ''}/?index|/?index\\.json)?`
: '(.json)?'
}`
if (nextConfig.basePath) { if (nextConfig.basePath) {
matcher = `${nextConfig.basePath}${matcher === '/' ? '' : matcher}` matcher = `${nextConfig.basePath}${matcher}`
} }
const parsedPage = tryToParsePath(matcher) const parsedPage = tryToParsePath(matcher)
if (parsedPage.error) { if (parsedPage.error) {
throw new Error(`Invalid path matcher: ${matcher}`) 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