Skip to content

Commit a678587

Browse files
fix(router-generator): fix layout/index conflict in virtual physical() routes
1 parent 2fa4cfa commit a678587

8 files changed

Lines changed: 147 additions & 2 deletions

File tree

‎packages/router-generator/src/filesystem/physical/getRouteNodes.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,20 @@ export async function getRouteNodes(
244244
originalRoutePath = '/'
245245
}
246246

247+
// For layout routes, don't use '/' fallback - an empty path means
248+
// "layout for the parent path" which is important for physical() mounts
249+
// where route.tsx at root should have empty path, not '/'
250+
const isLayoutRoute = routeType === 'layout'
251+
247252
routePath =
248253
routePath.replace(new RegExp(`/${config.indexToken}$`), '/') ||
249-
'/'
254+
(isLayoutRoute ? '' : '/')
250255

251256
originalRoutePath =
252257
originalRoutePath.replace(
253258
new RegExp(`/${config.indexToken}$`),
254259
'/',
255-
) || '/'
260+
) || (isLayoutRoute ? '' : '/')
256261
}
257262

258263
routeNodes.push({
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* eslint-disable */
2+
3+
// @ts-nocheck
4+
5+
// noinspection JSUnusedGlobalSymbols
6+
7+
// This file was automatically generated by TanStack Router.
8+
// You should NOT make any changes in this file as it will be overwritten.
9+
// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified.
10+
11+
import { Route as rootRouteImport } from './routes/__root'
12+
import { Route as FeatureRouteRouteImport } from './routes/feature/route'
13+
import { Route as IndexRouteImport } from './routes/index'
14+
import { Route as FeatureIndexRouteImport } from './routes/feature/index'
15+
16+
const FeatureRouteRoute = FeatureRouteRouteImport.update({
17+
id: '/feature',
18+
path: '/feature',
19+
getParentRoute: () => rootRouteImport,
20+
} as any)
21+
const IndexRoute = IndexRouteImport.update({
22+
id: '/',
23+
path: '/',
24+
getParentRoute: () => rootRouteImport,
25+
} as any)
26+
const FeatureIndexRoute = FeatureIndexRouteImport.update({
27+
id: '/',
28+
path: '/',
29+
getParentRoute: () => FeatureRouteRoute,
30+
} as any)
31+
32+
export interface FileRoutesByFullPath {
33+
'/': typeof IndexRoute
34+
'/feature': typeof FeatureRouteRouteWithChildren
35+
'/feature/': typeof FeatureIndexRoute
36+
}
37+
export interface FileRoutesByTo {
38+
'/': typeof IndexRoute
39+
'/feature': typeof FeatureIndexRoute
40+
}
41+
export interface FileRoutesById {
42+
__root__: typeof rootRouteImport
43+
'/': typeof IndexRoute
44+
'/feature': typeof FeatureRouteRouteWithChildren
45+
'/feature/': typeof FeatureIndexRoute
46+
}
47+
export interface FileRouteTypes {
48+
fileRoutesByFullPath: FileRoutesByFullPath
49+
fullPaths: '/' | '/feature' | '/feature/'
50+
fileRoutesByTo: FileRoutesByTo
51+
to: '/' | '/feature'
52+
id: '__root__' | '/' | '/feature' | '/feature/'
53+
fileRoutesById: FileRoutesById
54+
}
55+
export interface RootRouteChildren {
56+
IndexRoute: typeof IndexRoute
57+
FeatureRouteRoute: typeof FeatureRouteRouteWithChildren
58+
}
59+
60+
declare module '@tanstack/react-router' {
61+
interface FileRoutesByPath {
62+
'/feature': {
63+
id: '/feature'
64+
path: '/feature'
65+
fullPath: '/feature'
66+
preLoaderRoute: typeof FeatureRouteRouteImport
67+
parentRoute: typeof rootRouteImport
68+
}
69+
'/': {
70+
id: '/'
71+
path: '/'
72+
fullPath: '/'
73+
preLoaderRoute: typeof IndexRouteImport
74+
parentRoute: typeof rootRouteImport
75+
}
76+
'/feature/': {
77+
id: '/feature/'
78+
path: '/'
79+
fullPath: '/feature/'
80+
preLoaderRoute: typeof FeatureIndexRouteImport
81+
parentRoute: typeof FeatureRouteRoute
82+
}
83+
}
84+
}
85+
86+
interface FeatureRouteRouteChildren {
87+
FeatureIndexRoute: typeof FeatureIndexRoute
88+
}
89+
90+
const FeatureRouteRouteChildren: FeatureRouteRouteChildren = {
91+
FeatureIndexRoute: FeatureIndexRoute,
92+
}
93+
94+
const FeatureRouteRouteWithChildren = FeatureRouteRoute._addFileChildren(
95+
FeatureRouteRouteChildren,
96+
)
97+
98+
const rootRouteChildren: RootRouteChildren = {
99+
IndexRoute: IndexRoute,
100+
FeatureRouteRoute: FeatureRouteRouteWithChildren,
101+
}
102+
export const routeTree = rootRouteImport
103+
._addFileChildren(rootRouteChildren)
104+
._addFileTypes<FileRouteTypes>()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { index, physical, rootRoute } from '@tanstack/virtual-file-routes'
2+
3+
export const routes = rootRoute('__root.tsx', [
4+
index('index.tsx'),
5+
physical('/feature', 'feature'),
6+
])
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createRootRoute, Outlet } from '@tanstack/react-router'
2+
3+
export const Route = createRootRoute({
4+
component: () => <Outlet />,
5+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
3+
export const Route = createFileRoute('/feature/')({
4+
component: () => <div>Feature Index</div>,
5+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { createFileRoute, Outlet } from '@tanstack/react-router'
2+
3+
export const Route = createFileRoute('/feature')({
4+
component: () => (
5+
<div>
6+
<h1>Feature Layout</h1>
7+
<Outlet />
8+
</div>
9+
),
10+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
3+
export const Route = createFileRoute('/')({
4+
component: () => <div>Home</div>,
5+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"routesDirectory": "./routes",
3+
"generatedRouteTree": "./routeTree.gen.ts",
4+
"virtualRouteConfig": "./routes.ts"
5+
}

0 commit comments

Comments
 (0)