|
|
import { isMetadataPage } from './is-metadata-route' |
|
|
import path from '../../shared/lib/isomorphic/path' |
|
|
import { interpolateDynamicPath } from '../../server/server-utils' |
|
|
import { getNamedRouteRegex } from '../../shared/lib/router/utils/route-regex' |
|
|
import { djb2Hash } from '../../shared/lib/hash' |
|
|
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths' |
|
|
import { normalizePathSep } from '../../shared/lib/page-path/normalize-path-sep' |
|
|
import { |
|
|
isGroupSegment, |
|
|
isParallelRouteSegment, |
|
|
} from '../../shared/lib/segment' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getMetadataRouteSuffix(page: string) { |
|
|
|
|
|
|
|
|
|
|
|
const parentPathname = path.dirname(page) |
|
|
|
|
|
if (page.endsWith('/sitemap')) { |
|
|
return '' |
|
|
} |
|
|
|
|
|
|
|
|
let suffix = '' |
|
|
|
|
|
const segments = parentPathname.split('/') |
|
|
if ( |
|
|
segments.some((seg) => isGroupSegment(seg) || isParallelRouteSegment(seg)) |
|
|
) { |
|
|
|
|
|
suffix = djb2Hash(parentPathname).toString(36).slice(0, 6) |
|
|
} |
|
|
return suffix |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function fillMetadataSegment( |
|
|
segment: string, |
|
|
params: any, |
|
|
lastSegment: string |
|
|
) { |
|
|
const pathname = normalizeAppPath(segment) |
|
|
const routeRegex = getNamedRouteRegex(pathname, { |
|
|
prefixRouteKeys: false, |
|
|
}) |
|
|
const route = interpolateDynamicPath(pathname, params, routeRegex) |
|
|
const { name, ext } = path.parse(lastSegment) |
|
|
const pagePath = path.posix.join(segment, name) |
|
|
const suffix = getMetadataRouteSuffix(pagePath) |
|
|
const routeSuffix = suffix ? `-${suffix}` : '' |
|
|
|
|
|
return normalizePathSep(path.join(route, `${name}${routeSuffix}${ext}`)) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function normalizeMetadataRoute(page: string) { |
|
|
if (!isMetadataPage(page)) { |
|
|
return page |
|
|
} |
|
|
let route = page |
|
|
let suffix = '' |
|
|
if (page === '/robots') { |
|
|
route += '.txt' |
|
|
} else if (page === '/manifest') { |
|
|
route += '.webmanifest' |
|
|
} else { |
|
|
suffix = getMetadataRouteSuffix(page) |
|
|
} |
|
|
|
|
|
|
|
|
if (!route.endsWith('/route')) { |
|
|
const { dir, name: baseName, ext } = path.parse(route) |
|
|
route = path.posix.join( |
|
|
dir, |
|
|
`${baseName}${suffix ? `-${suffix}` : ''}${ext}`, |
|
|
'route' |
|
|
) |
|
|
} |
|
|
|
|
|
return route |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function normalizeMetadataPageToRoute(page: string, isDynamic: boolean) { |
|
|
const isRoute = page.endsWith('/route') |
|
|
const routePagePath = isRoute ? page.slice(0, -'/route'.length) : page |
|
|
const metadataRouteExtension = routePagePath.endsWith('/sitemap') |
|
|
? '.xml' |
|
|
: '' |
|
|
const mapped = isDynamic |
|
|
? `${routePagePath}/[__metadata_id__]` |
|
|
: `${routePagePath}${metadataRouteExtension}` |
|
|
|
|
|
return mapped + (isRoute ? '/route' : '') |
|
|
} |
|
|
|