silverwolf-x/project / packages /next /src /lib /generate-interception-routes-rewrites.ts
silverwolf-x's picture
download
raw
1.85 kB
import { NEXT_URL } from '../client/components/app-router-headers'
import {
extractInterceptionRouteInformation,
isInterceptionRouteAppPath,
} from '../shared/lib/router/utils/interception-routes'
import type { Rewrite } from './load-custom-routes'
import { getNamedRouteRegex } from '../shared/lib/router/utils/route-regex'
export function generateInterceptionRoutesRewrites(
appPaths: string[],
basePath = ''
): Rewrite[] {
const rewrites: Rewrite[] = []
for (const appPath of appPaths) {
if (isInterceptionRouteAppPath(appPath)) {
const { interceptingRoute, interceptedRoute } =
extractInterceptionRouteInformation(appPath)
const destination = getNamedRouteRegex(basePath + appPath, {
prefixRouteKeys: true,
})
const header = getNamedRouteRegex(interceptingRoute, {
prefixRouteKeys: true,
reference: destination.reference,
})
const source = getNamedRouteRegex(basePath + interceptedRoute, {
prefixRouteKeys: true,
reference: header.reference,
})
const headerRegex = header.namedRegex
// Strip ^ and $ anchors since matchHas() will add them automatically
.replace(/^\^/, '')
.replace(/\$$/, '')
// Replace matching the `/` with matching any route segment.
.replace(/^\/\(\?:\/\)\?$/, '/.*')
// Replace the optional trailing with slash capture group with one that
// will match any descendants.
.replace(/\(\?:\/\)\?$/, '(?:/.*)?')
rewrites.push({
source: source.pathToRegexpPattern,
destination: destination.pathToRegexpPattern,
has: [
{
type: 'header',
key: NEXT_URL,
value: headerRegex,
},
],
regex: source.namedRegex,
})
}
}
return rewrites
}

Xet Storage Details

Size:
1.85 kB
·
Xet hash:
1dc86980d7e34f0b2d33973478fed9097d1a1c4b31cac63a8eca87c6693f4563

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.