Skip to content

Commit c495f09

Browse files
committed
fix(router-generator): add virtual index route creation for empty layout routes
Signed-off-by: leesb971204 <[email protected]>
1 parent 415b821 commit c495f09

File tree

2 files changed

+63
-14
lines changed

2 files changed

+63
-14
lines changed

packages/router-generator/src/generator.ts

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -402,13 +402,6 @@ export async function generator(config: Config, root: string) {
402402
} else {
403403
routeTree.push(node)
404404
}
405-
if (
406-
node._fsRouteType === 'layout' &&
407-
node.isVirtual &&
408-
node.isVirtualParentRoute
409-
) {
410-
node.isNonPath = node.children?.every((d) => d.isNonPath)
411-
}
412405

413406
routeNodes.push(node)
414407
}
@@ -417,6 +410,29 @@ export async function generator(config: Config, root: string) {
417410
await handleNode(node)
418411
}
419412

413+
// Detect empty layout routes and add a virtual index route
414+
const layoutRoutesWithoutIndex = routeNodes.filter((node) => {
415+
if (node._fsRouteType !== 'layout') return false
416+
417+
if (node.children && node.children.length > 0) {
418+
if (node.children.some((child) => child.cleanedPath === '/')) {
419+
return false
420+
}
421+
}
422+
return true
423+
})
424+
425+
// Add a virtual index route to empty layout routes
426+
for (const layoutRoute of layoutRoutesWithoutIndex) {
427+
const virtualIndexRoute = createVirtualIndexRouteWithRedirect(layoutRoute)
428+
routeNodes.push(virtualIndexRoute)
429+
if (!layoutRoute.children) {
430+
layoutRoute.children = []
431+
}
432+
layoutRoute.children.push(virtualIndexRoute)
433+
virtualIndexRoute.parent = layoutRoute
434+
}
435+
420436
checkRouteFullPathUniqueness(
421437
preRouteNodes.filter(
422438
(d) =>
@@ -537,6 +553,7 @@ export async function generator(config: Config, root: string) {
537553

538554
const imports = Object.entries({
539555
createFileRoute: sortedRouteNodes.some((d) => d.isVirtual),
556+
redirect: routeNodes.some((node) => node.isVirtualRedirectIndex),
540557
lazyFn: sortedRouteNodes.some(
541558
(node) => routePiecesByPath[node.routePath!]?.loader,
542559
),
@@ -585,9 +602,14 @@ export async function generator(config: Config, root: string) {
585602
virtualRouteNodes.length ? '// Create Virtual Routes' : '',
586603
virtualRouteNodes
587604
.map((node) => {
588-
return `const ${
589-
node.variableName
590-
}Import = createFileRoute('${node.routePath}')()`
605+
if (node.isVirtualRedirectIndex) {
606+
return `const ${node.variableName}Import = createFileRoute('${node.routePath}')({
607+
loader: () => {
608+
redirect({ to: '/', throw: true })
609+
}
610+
})`
611+
}
612+
return `const ${node.variableName}Import = createFileRoute('${node.routePath}')()`
591613
})
592614
.join('\n'),
593615
'// Create/Update Routes',
@@ -843,10 +865,7 @@ function removeGroups(s: string) {
843865
*/
844866
function determineNodePath(node: RouteNode) {
845867
return (node.path = node.parent
846-
? node.routePath?.replace(
847-
node.parent.isNonPath ? '' : (node.parent.routePath ?? ''),
848-
'',
849-
) || '/'
868+
? node.routePath?.replace(node.parent.routePath ?? '', '') || '/'
850869
: node.routePath)
851870
}
852871

@@ -1105,3 +1124,31 @@ export function startAPIRouteSegmentsFromTSRFilePath(
11051124

11061125
return segments
11071126
}
1127+
1128+
/**
1129+
* Creates a virtual index route with a redirect to the root route.
1130+
*
1131+
* @param parentRoute - The parent layout route that does not have an index route.
1132+
* @returns The created virtual index route node.
1133+
*/
1134+
export const createVirtualIndexRouteWithRedirect = (
1135+
parentRoute: RouteNode,
1136+
): RouteNode => {
1137+
const indexRoutePath = `${parentRoute.routePath}/`
1138+
const variableName = routePathToVariable(`${indexRoutePath}index`)
1139+
1140+
const virtualIndexRoute: RouteNode = {
1141+
filePath: '',
1142+
fullPath: '',
1143+
variableName,
1144+
routePath: indexRoutePath,
1145+
path: '/',
1146+
cleanedPath: '/',
1147+
isVirtual: true,
1148+
_fsRouteType: 'static',
1149+
isVirtualRedirectIndex: true,
1150+
redirectTo: '/',
1151+
}
1152+
1153+
return virtualIndexRoute
1154+
}

packages/router-generator/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export type RouteNode = {
1212
isVirtual?: boolean
1313
children?: Array<RouteNode>
1414
parent?: RouteNode
15+
isVirtualRedirectIndex?: boolean
16+
redirectTo?: string
1517
}
1618

1719
export interface GetRouteNodesResult {

0 commit comments

Comments
 (0)