|
|
import { isInterceptionRouteAppPath } from '../shared/lib/router/utils/interception-routes' |
|
|
import { AppPathnameNormalizer } from '../server/normalizers/built/app/app-pathname-normalizer' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function normalizeCatchAllRoutes( |
|
|
appPaths: Record<string, string[]>, |
|
|
normalizer = new AppPathnameNormalizer() |
|
|
) { |
|
|
const catchAllRoutes = [ |
|
|
...new Set( |
|
|
Object.values(appPaths) |
|
|
.flat() |
|
|
.filter(isCatchAllRoute) |
|
|
|
|
|
.sort((a, b) => b.split('/').length - a.split('/').length) |
|
|
), |
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const filteredAppPaths = Object.keys(appPaths).filter( |
|
|
(route) => !isInterceptionRouteAppPath(route) |
|
|
) |
|
|
|
|
|
for (const appPath of filteredAppPaths) { |
|
|
for (const catchAllRoute of catchAllRoutes) { |
|
|
const normalizedCatchAllRoute = normalizer.normalize(catchAllRoute) |
|
|
const normalizedCatchAllRouteBasePath = normalizedCatchAllRoute.slice( |
|
|
0, |
|
|
normalizedCatchAllRoute.search(catchAllRouteRegex) |
|
|
) |
|
|
|
|
|
if ( |
|
|
|
|
|
appPath.startsWith(normalizedCatchAllRouteBasePath) && |
|
|
|
|
|
!appPaths[appPath].some((path) => hasMatchedSlots(path, catchAllRoute)) |
|
|
) { |
|
|
|
|
|
|
|
|
if (isOptionalCatchAll(catchAllRoute)) { |
|
|
|
|
|
|
|
|
appPaths[appPath].push(catchAllRoute) |
|
|
} else if (isCatchAll(catchAllRoute)) { |
|
|
|
|
|
|
|
|
if (normalizedCatchAllRouteBasePath !== appPath) { |
|
|
appPaths[appPath].push(catchAllRoute) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
function hasMatchedSlots(path1: string, path2: string): boolean { |
|
|
const slots1 = path1.split('/').filter(isMatchableSlot) |
|
|
const slots2 = path2.split('/').filter(isMatchableSlot) |
|
|
|
|
|
|
|
|
if (slots1.length !== slots2.length) return false |
|
|
|
|
|
|
|
|
for (let i = 0; i < slots1.length; i++) { |
|
|
if (slots1[i] !== slots2[i]) return false |
|
|
} |
|
|
|
|
|
return true |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isMatchableSlot(segment: string): boolean { |
|
|
return segment.startsWith('@') && segment !== '@children' |
|
|
} |
|
|
|
|
|
const catchAllRouteRegex = /\[?\[\.\.\./ |
|
|
|
|
|
function isCatchAllRoute(pathname: string): boolean { |
|
|
|
|
|
return !isOptionalCatchAll(pathname) && isCatchAll(pathname) |
|
|
} |
|
|
|
|
|
function isOptionalCatchAll(pathname: string): boolean { |
|
|
return pathname.includes('[[...') |
|
|
} |
|
|
|
|
|
function isCatchAll(pathname: string): boolean { |
|
|
return pathname.includes('[...') |
|
|
} |
|
|
|