53d1b00c7f
* fix the dynamic routing of middleware * add middleware to dynamicRoutes of routes-manifest * remove unused import * fix middleware routing with static paths * update manifest version test * prevent to match with api route using regex * use iterator instead of generator * do not use Iterator * fix type * fix type * remove unused import * apply the fix for support colons Co-authored-by: JJ Kasper <jj@jjsweb.site>
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import { getParametrizedRoute, RouteRegex } from './route-regex'
|
|
|
|
// Identify ^/[param]/ in route string
|
|
const FIRST_SEGMENT_DYNAMIC = /^\/\[[^/]+?\](?=\/|$)/
|
|
|
|
const NOT_API_ROUTE = '(?!/api(?:/|$))'
|
|
|
|
export function getMiddlewareRegex(
|
|
normalizedRoute: string,
|
|
catchAll: boolean = true
|
|
): RouteRegex {
|
|
const result = getParametrizedRoute(normalizedRoute)
|
|
const notApiRegex = FIRST_SEGMENT_DYNAMIC.test(normalizedRoute)
|
|
? NOT_API_ROUTE
|
|
: ''
|
|
|
|
let catchAllRegex = catchAll ? '(?!_next($|/)).*' : ''
|
|
let catchAllGroupedRegex = catchAll ? '(?:(/.*)?)' : ''
|
|
|
|
if ('routeKeys' in result) {
|
|
if (result.parameterizedRoute === '/') {
|
|
return {
|
|
groups: {},
|
|
namedRegex: `^/${catchAllRegex}$`,
|
|
re: new RegExp(`^/${catchAllRegex}$`),
|
|
routeKeys: {},
|
|
}
|
|
}
|
|
|
|
return {
|
|
groups: result.groups,
|
|
namedRegex: `^${notApiRegex}${result.namedParameterizedRoute}${catchAllGroupedRegex}$`,
|
|
re: new RegExp(
|
|
`^${notApiRegex}${result.parameterizedRoute}${catchAllGroupedRegex}$`
|
|
),
|
|
routeKeys: result.routeKeys,
|
|
}
|
|
}
|
|
|
|
if (result.parameterizedRoute === '/') {
|
|
return {
|
|
groups: {},
|
|
re: new RegExp(`^/${catchAllRegex}$`),
|
|
}
|
|
}
|
|
|
|
return {
|
|
groups: {},
|
|
re: new RegExp(
|
|
`^${notApiRegex}${result.parameterizedRoute}${catchAllGroupedRegex}$`
|
|
),
|
|
}
|
|
}
|