diff --git a/packages/router-generator/src/filesystem/physical/getRouteNodes.ts b/packages/router-generator/src/filesystem/physical/getRouteNodes.ts index 0c64052f6c..c603801483 100644 --- a/packages/router-generator/src/filesystem/physical/getRouteNodes.ts +++ b/packages/router-generator/src/filesystem/physical/getRouteNodes.ts @@ -244,15 +244,20 @@ export async function getRouteNodes( originalRoutePath = '/' } + // For layout routes, don't use '/' fallback - an empty path means + // "layout for the parent path" which is important for physical() mounts + // where route.tsx at root should have empty path, not '/' + const isLayoutRoute = routeType === 'layout' + routePath = routePath.replace(new RegExp(`/${config.indexToken}$`), '/') || - '/' + (isLayoutRoute ? '' : '/') originalRoutePath = originalRoutePath.replace( new RegExp(`/${config.indexToken}$`), '/', - ) || '/' + ) || (isLayoutRoute ? '' : '/') } routeNodes.push({ diff --git a/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routeTree.snapshot.ts b/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routeTree.snapshot.ts new file mode 100644 index 0000000000..2191ecf8b3 --- /dev/null +++ b/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routeTree.snapshot.ts @@ -0,0 +1,104 @@ +/* eslint-disable */ + +// @ts-nocheck + +// noinspection JSUnusedGlobalSymbols + +// This file was automatically generated by TanStack Router. +// You should NOT make any changes in this file as it will be overwritten. +// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified. + +import { Route as rootRouteImport } from './routes/__root' +import { Route as FeatureRouteRouteImport } from './routes/feature/route' +import { Route as IndexRouteImport } from './routes/index' +import { Route as FeatureIndexRouteImport } from './routes/feature/index' + +const FeatureRouteRoute = FeatureRouteRouteImport.update({ + id: '/feature', + path: '/feature', + getParentRoute: () => rootRouteImport, +} as any) +const IndexRoute = IndexRouteImport.update({ + id: '/', + path: '/', + getParentRoute: () => rootRouteImport, +} as any) +const FeatureIndexRoute = FeatureIndexRouteImport.update({ + id: '/', + path: '/', + getParentRoute: () => FeatureRouteRoute, +} as any) + +export interface FileRoutesByFullPath { + '/': typeof IndexRoute + '/feature': typeof FeatureRouteRouteWithChildren + '/feature/': typeof FeatureIndexRoute +} +export interface FileRoutesByTo { + '/': typeof IndexRoute + '/feature': typeof FeatureIndexRoute +} +export interface FileRoutesById { + __root__: typeof rootRouteImport + '/': typeof IndexRoute + '/feature': typeof FeatureRouteRouteWithChildren + '/feature/': typeof FeatureIndexRoute +} +export interface FileRouteTypes { + fileRoutesByFullPath: FileRoutesByFullPath + fullPaths: '/' | '/feature' | '/feature/' + fileRoutesByTo: FileRoutesByTo + to: '/' | '/feature' + id: '__root__' | '/' | '/feature' | '/feature/' + fileRoutesById: FileRoutesById +} +export interface RootRouteChildren { + IndexRoute: typeof IndexRoute + FeatureRouteRoute: typeof FeatureRouteRouteWithChildren +} + +declare module '@tanstack/react-router' { + interface FileRoutesByPath { + '/feature': { + id: '/feature' + path: '/feature' + fullPath: '/feature' + preLoaderRoute: typeof FeatureRouteRouteImport + parentRoute: typeof rootRouteImport + } + '/': { + id: '/' + path: '/' + fullPath: '/' + preLoaderRoute: typeof IndexRouteImport + parentRoute: typeof rootRouteImport + } + '/feature/': { + id: '/feature/' + path: '/' + fullPath: '/feature/' + preLoaderRoute: typeof FeatureIndexRouteImport + parentRoute: typeof FeatureRouteRoute + } + } +} + +interface FeatureRouteRouteChildren { + FeatureIndexRoute: typeof FeatureIndexRoute +} + +const FeatureRouteRouteChildren: FeatureRouteRouteChildren = { + FeatureIndexRoute: FeatureIndexRoute, +} + +const FeatureRouteRouteWithChildren = FeatureRouteRoute._addFileChildren( + FeatureRouteRouteChildren, +) + +const rootRouteChildren: RootRouteChildren = { + IndexRoute: IndexRoute, + FeatureRouteRoute: FeatureRouteRouteWithChildren, +} +export const routeTree = rootRouteImport + ._addFileChildren(rootRouteChildren) + ._addFileTypes() diff --git a/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routes.ts b/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routes.ts new file mode 100644 index 0000000000..b86d8ccdf1 --- /dev/null +++ b/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routes.ts @@ -0,0 +1,6 @@ +import { index, physical, rootRoute } from '@tanstack/virtual-file-routes' + +export const routes = rootRoute('__root.tsx', [ + index('index.tsx'), + physical('/feature', 'feature'), +]) diff --git a/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routes/__root.tsx b/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routes/__root.tsx new file mode 100644 index 0000000000..f463b796b4 --- /dev/null +++ b/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routes/__root.tsx @@ -0,0 +1,5 @@ +import { createRootRoute, Outlet } from '@tanstack/react-router' + +export const Route = createRootRoute({ + component: () => , +}) diff --git a/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routes/feature/index.tsx b/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routes/feature/index.tsx new file mode 100644 index 0000000000..bd8bf1553e --- /dev/null +++ b/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routes/feature/index.tsx @@ -0,0 +1,5 @@ +import { createFileRoute } from '@tanstack/react-router' + +export const Route = createFileRoute('/feature/')({ + component: () =>
Feature Index
, +}) diff --git a/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routes/feature/route.tsx b/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routes/feature/route.tsx new file mode 100644 index 0000000000..9ad7fad8ab --- /dev/null +++ b/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routes/feature/route.tsx @@ -0,0 +1,10 @@ +import { createFileRoute, Outlet } from '@tanstack/react-router' + +export const Route = createFileRoute('/feature')({ + component: () => ( +
+

Feature Layout

+ +
+ ), +}) diff --git a/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routes/index.tsx b/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routes/index.tsx new file mode 100644 index 0000000000..fa6ed84966 --- /dev/null +++ b/packages/router-generator/tests/generator/virtual-physical-layout-and-index/routes/index.tsx @@ -0,0 +1,5 @@ +import { createFileRoute } from '@tanstack/react-router' + +export const Route = createFileRoute('/')({ + component: () =>
Home
, +}) diff --git a/packages/router-generator/tests/generator/virtual-physical-layout-and-index/tsr.config.json b/packages/router-generator/tests/generator/virtual-physical-layout-and-index/tsr.config.json new file mode 100644 index 0000000000..4d587108e3 --- /dev/null +++ b/packages/router-generator/tests/generator/virtual-physical-layout-and-index/tsr.config.json @@ -0,0 +1,5 @@ +{ + "routesDirectory": "./routes", + "generatedRouteTree": "./routeTree.gen.ts", + "virtualRouteConfig": "./routes.ts" +}