diff --git a/packages/router-generator/src/utils.ts b/packages/router-generator/src/utils.ts index 9fa8d229a4..1f00d37999 100644 --- a/packages/router-generator/src/utils.ts +++ b/packages/router-generator/src/utils.ts @@ -19,6 +19,19 @@ export class RoutePrefixMap { for (const route of routes) { if (!route.routePath || route.routePath === `/${rootPathId}`) continue + // Skip route pieces (lazy, loader, component, etc.) - they are merged with main routes + // and should not be valid parent candidates + if ( + route._fsRouteType === 'lazy' || + route._fsRouteType === 'loader' || + route._fsRouteType === 'component' || + route._fsRouteType === 'pendingComponent' || + route._fsRouteType === 'errorComponent' || + route._fsRouteType === 'notFoundComponent' + ) { + continue + } + // Index by exact path for direct lookups this.prefixToRoute.set(route.routePath, route) diff --git a/packages/router-generator/tests/generator/lazy/routeTree.snapshot.ts b/packages/router-generator/tests/generator/lazy/routeTree.snapshot.ts new file mode 100644 index 0000000000..ea40f442ad --- /dev/null +++ b/packages/router-generator/tests/generator/lazy/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 PostsRouteRouteImport } from './routes/posts.route' +import { Route as PostsIndexRouteImport } from './routes/posts.index' +import { Route as PostsPostIdRouteImport } from './routes/posts.$postId' + +const PostsRouteRoute = PostsRouteRouteImport.update({ + id: '/posts', + path: '/posts', + getParentRoute: () => rootRouteImport, +} as any).lazy(() => import('./routes/posts.route.lazy').then((d) => d.Route)) +const PostsIndexRoute = PostsIndexRouteImport.update({ + id: '/', + path: '/', + getParentRoute: () => PostsRouteRoute, +} as any) +const PostsPostIdRoute = PostsPostIdRouteImport.update({ + id: '/$postId', + path: '/$postId', + getParentRoute: () => PostsRouteRoute, +} as any) + +export interface FileRoutesByFullPath { + '/posts': typeof PostsRouteRouteWithChildren + '/posts/$postId': typeof PostsPostIdRoute + '/posts/': typeof PostsIndexRoute +} +export interface FileRoutesByTo { + '/posts/$postId': typeof PostsPostIdRoute + '/posts': typeof PostsIndexRoute +} +export interface FileRoutesById { + __root__: typeof rootRouteImport + '/posts': typeof PostsRouteRouteWithChildren + '/posts/$postId': typeof PostsPostIdRoute + '/posts/': typeof PostsIndexRoute +} +export interface FileRouteTypes { + fileRoutesByFullPath: FileRoutesByFullPath + fullPaths: '/posts' | '/posts/$postId' | '/posts/' + fileRoutesByTo: FileRoutesByTo + to: '/posts/$postId' | '/posts' + id: '__root__' | '/posts' | '/posts/$postId' | '/posts/' + fileRoutesById: FileRoutesById +} +export interface RootRouteChildren { + PostsRouteRoute: typeof PostsRouteRouteWithChildren +} + +declare module '@tanstack/react-router' { + interface FileRoutesByPath { + '/posts': { + id: '/posts' + path: '/posts' + fullPath: '/posts' + preLoaderRoute: typeof PostsRouteRouteImport + parentRoute: typeof rootRouteImport + } + '/posts/': { + id: '/posts/' + path: '/' + fullPath: '/posts/' + preLoaderRoute: typeof PostsIndexRouteImport + parentRoute: typeof PostsRouteRoute + } + '/posts/$postId': { + id: '/posts/$postId' + path: '/$postId' + fullPath: '/posts/$postId' + preLoaderRoute: typeof PostsPostIdRouteImport + parentRoute: typeof PostsRouteRoute + } + } +} + +interface PostsRouteRouteChildren { + PostsPostIdRoute: typeof PostsPostIdRoute + PostsIndexRoute: typeof PostsIndexRoute +} + +const PostsRouteRouteChildren: PostsRouteRouteChildren = { + PostsPostIdRoute: PostsPostIdRoute, + PostsIndexRoute: PostsIndexRoute, +} + +const PostsRouteRouteWithChildren = PostsRouteRoute._addFileChildren( + PostsRouteRouteChildren, +) + +const rootRouteChildren: RootRouteChildren = { + PostsRouteRoute: PostsRouteRouteWithChildren, +} +export const routeTree = rootRouteImport + ._addFileChildren(rootRouteChildren) + ._addFileTypes() diff --git a/packages/router-generator/tests/generator/lazy/routes/__root.tsx b/packages/router-generator/tests/generator/lazy/routes/__root.tsx new file mode 100644 index 0000000000..3797d7bf8c --- /dev/null +++ b/packages/router-generator/tests/generator/lazy/routes/__root.tsx @@ -0,0 +1,15 @@ +import * as React from 'react' +import { Outlet, createRootRoute } from '@tanstack/react-router' + +export const Route = createRootRoute({ + component: RootComponent, +}) + +function RootComponent() { + return ( + +
Hello "__root"!
+ +
+ ) +} diff --git a/packages/router-generator/tests/generator/lazy/routes/posts.$postId.tsx b/packages/router-generator/tests/generator/lazy/routes/posts.$postId.tsx new file mode 100644 index 0000000000..8782ec97d8 --- /dev/null +++ b/packages/router-generator/tests/generator/lazy/routes/posts.$postId.tsx @@ -0,0 +1,9 @@ +import { createFileRoute } from '@tanstack/react-router' + +export const Route = createFileRoute('/posts/$postId')({ + component: RouteComponent, +}) + +function RouteComponent() { + return
Hello "/posts/$postId"!
+} diff --git a/packages/router-generator/tests/generator/lazy/routes/posts.index.tsx b/packages/router-generator/tests/generator/lazy/routes/posts.index.tsx new file mode 100644 index 0000000000..edd51af264 --- /dev/null +++ b/packages/router-generator/tests/generator/lazy/routes/posts.index.tsx @@ -0,0 +1,9 @@ +import { createFileRoute } from '@tanstack/react-router' + +export const Route = createFileRoute('/posts/')({ + component: RouteComponent, +}) + +function RouteComponent() { + return
Hello "/posts/"!
+} diff --git a/packages/router-generator/tests/generator/lazy/routes/posts.route.lazy.tsx b/packages/router-generator/tests/generator/lazy/routes/posts.route.lazy.tsx new file mode 100644 index 0000000000..906c2167dc --- /dev/null +++ b/packages/router-generator/tests/generator/lazy/routes/posts.route.lazy.tsx @@ -0,0 +1,9 @@ +import { createLazyFileRoute } from '@tanstack/react-router' + +export const Route = createLazyFileRoute('/posts')({ + component: RouteComponent, +}) + +function RouteComponent() { + return
Hello "/posts"!
+} diff --git a/packages/router-generator/tests/generator/lazy/routes/posts.route.tsx b/packages/router-generator/tests/generator/lazy/routes/posts.route.tsx new file mode 100644 index 0000000000..929cb15377 --- /dev/null +++ b/packages/router-generator/tests/generator/lazy/routes/posts.route.tsx @@ -0,0 +1,9 @@ +import { createFileRoute } from '@tanstack/react-router' + +export const Route = createFileRoute('/posts')({ + component: RouteComponent, +}) + +function RouteComponent() { + return
Hello "/posts"!
+}