Skip to content

Commit

Permalink
fix segment collection to account for parallel routes
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Dec 10, 2024
1 parent 8242989 commit 25aca48
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions packages/next/src/build/segment-config/app/app-segments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { isClientReference } from '../../../lib/client-reference'
import { getSegmentParam } from '../../../server/app-render/get-segment-param'
import { getLayoutOrPageModule } from '../../../server/lib/app-dir-module'
import { PAGE_SEGMENT_KEY } from '../../../shared/lib/segment'

type GenerateStaticParams = (options: { params?: Params }) => Promise<Params[]>

Expand Down Expand Up @@ -75,13 +76,16 @@ export type AppSegment = {
async function collectAppPageSegments(routeModule: AppPageRouteModule) {
const segments: AppSegment[] = []

let current = routeModule.userland.loaderTree
while (current) {
const [name, parallelRoutes] = current
const { mod: userland, filePath } = await getLayoutOrPageModule(current)
// Helper function to process a loader tree path
async function processLoaderTree(
loaderTree: any,
currentSegments: AppSegment[] = []
): Promise<void> {
const [name, parallelRoutes] = loaderTree
const { mod: userland, filePath } = await getLayoutOrPageModule(loaderTree)

const isClientComponent: boolean = userland && isClientReference(userland)
const isDynamicSegment = /^\[.*\]$/.test(name)
const isDynamicSegment = /\[.*\]$/.test(name)
const param = isDynamicSegment ? getSegmentParam(name)?.param : undefined

const segment: AppSegment = {
Expand All @@ -100,12 +104,22 @@ async function collectAppPageSegments(routeModule: AppPageRouteModule) {
attach(segment, userland, routeModule.definition.pathname)
}

segments.push(segment)
currentSegments.push(segment)

// If this is a page segment, we know we've reached a leaf node associated with the
// page we're collecting segments for. We can add the collected segments to our final result.
if (name === PAGE_SEGMENT_KEY) {
segments.push(...currentSegments)
}

// Use this route's parallel route children as the next segment.
current = parallelRoutes.children
// Recursively process parallel routes
for (const parallelRouteKey in parallelRoutes) {
const parallelRoute = parallelRoutes[parallelRouteKey]
await processLoaderTree(parallelRoute, [...currentSegments])
}
}

await processLoaderTree(routeModule.userland.loaderTree)
return segments
}

Expand Down

0 comments on commit 25aca48

Please sign in to comment.