Skip to content

Commit

Permalink
feat(middleware): augments / matcher with /index (#39397)
Browse files Browse the repository at this point in the history
* 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: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
feugy and ijjk committed Aug 9, 2022
1 parent e2119c0 commit 15a216b
Show file tree
Hide file tree
Showing 2 changed files with 239 additions and 103 deletions.
21 changes: 12 additions & 9 deletions packages/next/build/analysis/get-page-static-info.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -185,6 +184,7 @@ function getMiddlewareRegExpStrings(
getMiddlewareRegExpStrings(matcher, nextConfig)
)
}
const { i18n } = nextConfig

if (typeof matcherOrMatchers !== 'string') {
throw new Error(
Expand All @@ -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}`)
}
Expand Down

0 comments on commit 15a216b

Please sign in to comment.