@@ -402,13 +402,6 @@ export async function generator(config: Config, root: string) {
402
402
} else {
403
403
routeTree . push ( node )
404
404
}
405
- if (
406
- node . _fsRouteType === 'layout' &&
407
- node . isVirtual &&
408
- node . isVirtualParentRoute
409
- ) {
410
- node . isNonPath = node . children ?. every ( ( d ) => d . isNonPath )
411
- }
412
405
413
406
routeNodes . push ( node )
414
407
}
@@ -417,6 +410,29 @@ export async function generator(config: Config, root: string) {
417
410
await handleNode ( node )
418
411
}
419
412
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
+
420
436
checkRouteFullPathUniqueness (
421
437
preRouteNodes . filter (
422
438
( d ) =>
@@ -537,6 +553,7 @@ export async function generator(config: Config, root: string) {
537
553
538
554
const imports = Object . entries ( {
539
555
createFileRoute : sortedRouteNodes . some ( ( d ) => d . isVirtual ) ,
556
+ redirect : routeNodes . some ( ( node ) => node . isVirtualRedirectIndex ) ,
540
557
lazyFn : sortedRouteNodes . some (
541
558
( node ) => routePiecesByPath [ node . routePath ! ] ?. loader ,
542
559
) ,
@@ -585,9 +602,14 @@ export async function generator(config: Config, root: string) {
585
602
virtualRouteNodes . length ? '// Create Virtual Routes' : '' ,
586
603
virtualRouteNodes
587
604
. 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 } ')()`
591
613
} )
592
614
. join ( '\n' ) ,
593
615
'// Create/Update Routes' ,
@@ -843,10 +865,7 @@ function removeGroups(s: string) {
843
865
*/
844
866
function determineNodePath ( node : RouteNode ) {
845
867
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 ?? '' , '' ) || '/'
850
869
: node . routePath )
851
870
}
852
871
@@ -1105,3 +1124,31 @@ export function startAPIRouteSegmentsFromTSRFilePath(
1105
1124
1106
1125
return segments
1107
1126
}
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
+ }
0 commit comments