|
|
import type { LoadComponentsReturnType } from '../../../server/load-components' |
|
|
import type { Params } from '../../../server/request/params' |
|
|
import type { |
|
|
AppPageRouteModule, |
|
|
AppPageModule, |
|
|
} from '../../../server/route-modules/app-page/module.compiled' |
|
|
import type { |
|
|
AppRouteRouteModule, |
|
|
AppRouteModule, |
|
|
} from '../../../server/route-modules/app-route/module.compiled' |
|
|
import { |
|
|
type AppSegmentConfig, |
|
|
parseAppSegmentConfig, |
|
|
} from './app-segment-config' |
|
|
|
|
|
import { InvariantError } from '../../../shared/lib/invariant-error' |
|
|
import { |
|
|
isAppRouteRouteModule, |
|
|
isAppPageRouteModule, |
|
|
} from '../../../server/route-modules/checks' |
|
|
import { isClientReference } from '../../../lib/client-and-server-references' |
|
|
import { getSegmentParam } from '../../../server/app-render/get-segment-param' |
|
|
import { |
|
|
getLayoutOrPageModule, |
|
|
type LoaderTree, |
|
|
} from '../../../server/lib/app-dir-module' |
|
|
import { PAGE_SEGMENT_KEY } from '../../../shared/lib/segment' |
|
|
|
|
|
type GenerateStaticParams = (options: { params?: Params }) => Promise<Params[]> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function attach(segment: AppSegment, userland: unknown, route: string) { |
|
|
|
|
|
if (typeof userland !== 'object' || userland === null) { |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
const config = parseAppSegmentConfig(userland, route) |
|
|
|
|
|
|
|
|
if (Object.keys(config).length > 0) { |
|
|
segment.config = config |
|
|
} |
|
|
|
|
|
if ( |
|
|
'generateStaticParams' in userland && |
|
|
typeof userland.generateStaticParams === 'function' |
|
|
) { |
|
|
segment.generateStaticParams = |
|
|
userland.generateStaticParams as GenerateStaticParams |
|
|
|
|
|
|
|
|
if (segment.config?.runtime === 'edge') { |
|
|
throw new Error( |
|
|
'Edge runtime is not supported with `generateStaticParams`.' |
|
|
) |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
export type AppSegment = { |
|
|
name: string |
|
|
param: string | undefined |
|
|
filePath: string | undefined |
|
|
config: AppSegmentConfig | undefined |
|
|
isDynamicSegment: boolean |
|
|
generateStaticParams: GenerateStaticParams | undefined |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function collectAppPageSegments(routeModule: AppPageRouteModule) { |
|
|
|
|
|
|
|
|
const uniqueSegments = new Map<string, AppSegment>() |
|
|
|
|
|
|
|
|
type QueueItem = [LoaderTree, AppSegment[]] |
|
|
const queue: QueueItem[] = [[routeModule.userland.loaderTree, []]] |
|
|
|
|
|
while (queue.length > 0) { |
|
|
const [loaderTree, currentSegments] = queue.shift()! |
|
|
const [name, parallelRoutes] = loaderTree |
|
|
|
|
|
|
|
|
const { mod: userland, filePath } = await getLayoutOrPageModule(loaderTree) |
|
|
const isClientComponent = userland && isClientReference(userland) |
|
|
|
|
|
const param = getSegmentParam(name)?.param |
|
|
|
|
|
const segment: AppSegment = { |
|
|
name, |
|
|
param, |
|
|
filePath, |
|
|
config: undefined, |
|
|
isDynamicSegment: !!param, |
|
|
generateStaticParams: undefined, |
|
|
} |
|
|
|
|
|
|
|
|
if (!isClientComponent) { |
|
|
attach(segment, userland, routeModule.definition.pathname) |
|
|
} |
|
|
|
|
|
|
|
|
const segmentKey = getSegmentKey(segment) |
|
|
if (!uniqueSegments.has(segmentKey)) { |
|
|
uniqueSegments.set(segmentKey, segment) |
|
|
} |
|
|
|
|
|
const updatedSegments = [...currentSegments, segment] |
|
|
|
|
|
|
|
|
if (name === PAGE_SEGMENT_KEY) { |
|
|
|
|
|
updatedSegments.forEach((seg) => { |
|
|
const key = getSegmentKey(seg) |
|
|
uniqueSegments.set(key, seg) |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
for (const parallelRouteKey in parallelRoutes) { |
|
|
const parallelRoute = parallelRoutes[parallelRouteKey] |
|
|
queue.push([parallelRoute, updatedSegments]) |
|
|
} |
|
|
} |
|
|
|
|
|
return Array.from(uniqueSegments.values()) |
|
|
} |
|
|
|
|
|
function getSegmentKey(segment: AppSegment) { |
|
|
return `${segment.name}-${segment.filePath ?? ''}-${segment.param ?? ''}` |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function collectAppRouteSegments( |
|
|
routeModule: AppRouteRouteModule |
|
|
): AppSegment[] { |
|
|
|
|
|
const parts = routeModule.definition.pathname.split('/').slice(1) |
|
|
if (parts.length === 0) { |
|
|
throw new InvariantError('Expected at least one segment') |
|
|
} |
|
|
|
|
|
|
|
|
const segments: AppSegment[] = parts.map((name) => { |
|
|
const param = getSegmentParam(name)?.param |
|
|
|
|
|
return { |
|
|
name, |
|
|
param, |
|
|
filePath: undefined, |
|
|
isDynamicSegment: !!param, |
|
|
config: undefined, |
|
|
generateStaticParams: undefined, |
|
|
} |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
const segment = segments[segments.length - 1] |
|
|
|
|
|
segment.filePath = routeModule.definition.filename |
|
|
|
|
|
|
|
|
attach(segment, routeModule.userland, routeModule.definition.pathname) |
|
|
|
|
|
return segments |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function collectSegments({ |
|
|
routeModule, |
|
|
}: LoadComponentsReturnType<AppPageModule | AppRouteModule>): |
|
|
| Promise<AppSegment[]> |
|
|
| AppSegment[] { |
|
|
if (isAppRouteRouteModule(routeModule)) { |
|
|
return collectAppRouteSegments(routeModule) |
|
|
} |
|
|
|
|
|
if (isAppPageRouteModule(routeModule)) { |
|
|
return collectAppPageSegments(routeModule) |
|
|
} |
|
|
|
|
|
throw new InvariantError( |
|
|
'Expected a route module to be one of app route or page' |
|
|
) |
|
|
} |
|
|
|