File size: 2,118 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import path from '../../../shared/lib/isomorphic/path'
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path'
import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex'
import {
RSC_PREFETCH_SUFFIX,
RSC_SEGMENT_SUFFIX,
RSC_SEGMENTS_DIR_SUFFIX,
} from '../../../lib/constants'
export const SEGMENT_PATH_KEY = 'nextSegmentPath'
export type PrefetchSegmentDataRoute = {
source: string
destination: string
routeKeys: { [key: string]: string }
}
export function buildPrefetchSegmentDataRoute(
page: string,
segmentPath: string
): PrefetchSegmentDataRoute {
const pagePath = normalizePagePath(page)
const destination = path.posix.join(
`${pagePath}${RSC_SEGMENTS_DIR_SUFFIX}`,
`${segmentPath}${RSC_SEGMENT_SUFFIX}`
)
const { namedRegex, routeKeys } = getNamedRouteRegex(destination, {
prefixRouteKeys: true,
includePrefix: true,
includeSuffix: true,
excludeOptionalTrailingSlash: true,
backreferenceDuplicateKeys: true,
})
return {
destination,
source: namedRegex,
routeKeys,
}
}
/**
* Builds a prefetch segment data route that is inverted. This means that it's
* supposed to rewrite from the previous segment paths route back to the
* prefetch RSC route.
*
* @param page - The page to build the route for.
* @param segmentPath - The segment path to build the route for.
* @returns The prefetch segment data route.
*/
export function buildInversePrefetchSegmentDataRoute(
page: string,
segmentPath: string
): PrefetchSegmentDataRoute {
const pagePath = normalizePagePath(page)
const source = path.posix.join(
`${pagePath}${RSC_SEGMENTS_DIR_SUFFIX}`,
`${segmentPath}${RSC_SEGMENT_SUFFIX}`
)
const { namedRegex, routeKeys } = getNamedRouteRegex(source, {
prefixRouteKeys: true,
includePrefix: true,
includeSuffix: true,
excludeOptionalTrailingSlash: true,
backreferenceDuplicateKeys: true,
})
const destination = path.posix.join(`${pagePath}${RSC_PREFETCH_SUFFIX}`)
return {
source: namedRegex,
destination,
routeKeys,
}
}
|