Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions packages/router-generator/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1422,19 +1422,33 @@ ${acc.routeTree.map((child) => `${child.variableName}Route: typeof ${getResolved
) {
let parentRoute = hasParentRoute(prefixMap, node, node.routePath)

// Fallback: check acc.routeNodesByPath for parents not in prefixMap
// This handles virtual routes created from lazy-only files that weren't
// in the initial prefixMap build
if (!parentRoute && node.routePath) {
let searchPath = node.routePath
while (searchPath.length > 0) {
const lastSlash = searchPath.lastIndexOf('/')
if (lastSlash <= 0) break
searchPath = searchPath.substring(0, lastSlash)
const candidate = acc.routeNodesByPath.get(searchPath)
if (candidate && candidate.routePath !== node.routePath) {
// Check routeNodesByPath for a closer parent that may not be in prefixMap.
//
// Why: The prefixMap excludes lazy routes by design. When lazy-only routes are
// nested inside a pathless layout, the virtual route created from the lazy file
// won't be in the prefixMap, but it will be in routeNodesByPath.
//
// Example: Given files _layout/path.lazy.tsx and _layout/path.index.lazy.tsx:
// - prefixMap contains: /_layout (from route.tsx)
// - routeNodesByPath contains: /_layout AND /_layout/path (virtual from lazy)
// - For /_layout/path/, hasParentRoute returns /_layout (wrong)
// - But the correct parent is /_layout/path (the virtual route from path.lazy.tsx)
//
// Optimization: Only search if we might find a closer parent. The search walks
// up from the immediate parent path, so if the first candidate matches what
// prefixMap found, there's no closer parent to find.
if (node.routePath) {
const lastSlash = node.routePath.lastIndexOf('/')
if (lastSlash > 0) {
const immediateParentPath = node.routePath.substring(0, lastSlash)
const candidate = acc.routeNodesByPath.get(immediateParentPath)
if (
candidate &&
candidate.routePath !== node.routePath &&
candidate !== parentRoute
) {
// Found a closer parent in routeNodesByPath that differs from prefixMap result
parentRoute = candidate
break
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/* 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 { createFileRoute } from '@tanstack/react-router'

import { Route as rootRouteImport } from './routes/__root'
import { Route as LayoutRouteRouteImport } from './routes/_layout/route'

const LayoutPathLazyRouteImport = createFileRoute('/_layout/path')()
const LayoutPathIndexLazyRouteImport = createFileRoute('/_layout/path/')()

const LayoutRouteRoute = LayoutRouteRouteImport.update({
id: '/_layout',
getParentRoute: () => rootRouteImport,
} as any)
const LayoutPathLazyRoute = LayoutPathLazyRouteImport.update({
id: '/path',
path: '/path',
getParentRoute: () => LayoutRouteRoute,
} as any).lazy(() => import('./routes/_layout/path.lazy').then((d) => d.Route))
const LayoutPathIndexLazyRoute = LayoutPathIndexLazyRouteImport.update({
id: '/',
path: '/',
getParentRoute: () => LayoutPathLazyRoute,
} as any).lazy(() =>
import('./routes/_layout/path.index.lazy').then((d) => d.Route),
)

export interface FileRoutesByFullPath {
'/': typeof LayoutRouteRouteWithChildren
'/path': typeof LayoutPathLazyRouteWithChildren
'/path/': typeof LayoutPathIndexLazyRoute
}
export interface FileRoutesByTo {
'/': typeof LayoutRouteRouteWithChildren
'/path': typeof LayoutPathIndexLazyRoute
}
export interface FileRoutesById {
__root__: typeof rootRouteImport
'/_layout': typeof LayoutRouteRouteWithChildren
'/_layout/path': typeof LayoutPathLazyRouteWithChildren
'/_layout/path/': typeof LayoutPathIndexLazyRoute
}
export interface FileRouteTypes {
fileRoutesByFullPath: FileRoutesByFullPath
fullPaths: '/' | '/path' | '/path/'
fileRoutesByTo: FileRoutesByTo
to: '/' | '/path'
id: '__root__' | '/_layout' | '/_layout/path' | '/_layout/path/'
fileRoutesById: FileRoutesById
}
export interface RootRouteChildren {
LayoutRouteRoute: typeof LayoutRouteRouteWithChildren
}

declare module '@tanstack/react-router' {
interface FileRoutesByPath {
'/_layout': {
id: '/_layout'
path: ''
fullPath: '/'
preLoaderRoute: typeof LayoutRouteRouteImport
parentRoute: typeof rootRouteImport
}
'/_layout/path': {
id: '/_layout/path'
path: '/path'
fullPath: '/path'
preLoaderRoute: typeof LayoutPathLazyRouteImport
parentRoute: typeof LayoutRouteRoute
}
'/_layout/path/': {
id: '/_layout/path/'
path: '/'
fullPath: '/path/'
preLoaderRoute: typeof LayoutPathIndexLazyRouteImport
parentRoute: typeof LayoutPathLazyRoute
}
}
}

interface LayoutPathLazyRouteChildren {
LayoutPathIndexLazyRoute: typeof LayoutPathIndexLazyRoute
}

const LayoutPathLazyRouteChildren: LayoutPathLazyRouteChildren = {
LayoutPathIndexLazyRoute: LayoutPathIndexLazyRoute,
}

const LayoutPathLazyRouteWithChildren = LayoutPathLazyRoute._addFileChildren(
LayoutPathLazyRouteChildren,
)

interface LayoutRouteRouteChildren {
LayoutPathLazyRoute: typeof LayoutPathLazyRouteWithChildren
}

const LayoutRouteRouteChildren: LayoutRouteRouteChildren = {
LayoutPathLazyRoute: LayoutPathLazyRouteWithChildren,
}

const LayoutRouteRouteWithChildren = LayoutRouteRoute._addFileChildren(
LayoutRouteRouteChildren,
)

const rootRouteChildren: RootRouteChildren = {
LayoutRouteRoute: LayoutRouteRouteWithChildren,
}
export const routeTree = rootRouteImport
._addFileChildren(rootRouteChildren)
._addFileTypes<FileRouteTypes>()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createRootRoute } from '@tanstack/react-router'

export const Route = createRootRoute()
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createLazyFileRoute } from '@tanstack/react-router'

export const Route = createLazyFileRoute('/_layout/path/')({
component: () => 'Path Index',
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createLazyFileRoute } from '@tanstack/react-router'

export const Route = createLazyFileRoute('/_layout/path')({
component: () => 'Path Layout',
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Outlet, createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/_layout')({
component: RouteComponent,
})

function RouteComponent() {
return <Outlet />
}
Loading