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
5 changes: 5 additions & 0 deletions .changeset/bright-bats-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/router-generator': patch
---

Preserve escaped underscore segments when generating index routes across physical and virtual route trees, including pathless layouts, `physical()` prefixes, and `__virtual.ts` subtrees.
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,26 @@ export async function getRouteNodes(
const filePath = replaceBackslash(
path.join(normalizedDir, node.filePath),
)
const prefixPath = cleanPath(`/${normalizedDir}`)
const routePath = cleanPath(`/${normalizedDir}${node.routePath}`)
const { routePath: prefixPath, originalRoutePath: originalPrefixPath } =
normalizedDir
? determineInitialRoutePath(normalizedDir)
: { routePath: '', originalRoutePath: '' }
const routePath = cleanPath(`${prefixPath}${node.routePath}`)

node.variableName = routePathToVariable(
cleanPath(`/${normalizedDir}/${removeExt(node.filePath)}`),
cleanPath(`${prefixPath}/${removeExt(node.filePath)}`),
)
node._routePathSegmentMetadata = joinRoutePathSegmentMetadata(
routePath,
prefixPath,
undefined,
createRoutePathSegmentMetadata(prefixPath, originalPrefixPath),
node._routePathSegmentMetadata,
)
node.routePath = routePath
// Keep originalRoutePath aligned with routePath for escape detection
if (node.originalRoutePath) {
node.originalRoutePath = cleanPath(
`/${normalizedDir}${node.originalRoutePath}`,
`${originalPrefixPath}${node.originalRoutePath}`,
)
}
node.filePath = filePath
Expand Down Expand Up @@ -313,10 +316,6 @@ export async function getRouteNodes(
routePath = '/'
}

if (lastOriginalSegment === updatedLastRouteSegment) {
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 '/'
Expand Down
20 changes: 17 additions & 3 deletions packages/router-generator/src/filesystem/virtual/getRouteNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ export async function getRouteNodesRecursive(
const children = await Promise.all(
nodes.map(async (node) => {
if (node.type === 'physical') {
const {
routePath: routePathPrefix,
originalRoutePath: originalRoutePathPrefix,
} = node.pathPrefix
? determineInitialRoutePath(removeLeadingSlash(node.pathPrefix))
: { routePath: '', originalRoutePath: '' }
const { routeNodes, physicalDirectories } = await getRouteNodesPhysical(
{
...tsrConfig,
Expand All @@ -166,13 +172,16 @@ export async function getRouteNodesRecursive(
)
routeNodes.forEach((subtreeNode) => {
const pathPrefix = cleanPath(
`${parent?.routePath ?? ''}${node.pathPrefix}`,
`${parent?.routePath ?? ''}${routePathPrefix}`,
)
const originalPathPrefix = cleanPath(
`${parent?.originalRoutePath ?? parent?.routePath ?? ''}${originalRoutePathPrefix}`,
)
const literalPathPrefixSegments =
createLiteralRoutePathSegmentMetadata(pathPrefix, parent, true)
const routePath = cleanPath(`${pathPrefix}${subtreeNode.routePath}`)
subtreeNode.variableName = routePathToVariable(
`${node.pathPrefix}/${removeExt(subtreeNode.filePath)}`,
`${routePathPrefix}/${removeExt(subtreeNode.filePath)}`,
)
subtreeNode._routePathSegmentMetadata = joinRoutePathSegmentMetadata(
routePath,
Expand All @@ -184,7 +193,7 @@ export async function getRouteNodesRecursive(
// Keep originalRoutePath aligned with routePath for escape detection
if (subtreeNode.originalRoutePath) {
subtreeNode.originalRoutePath = cleanPath(
`${parent?.originalRoutePath ?? parent?.routePath ?? ''}${node.pathPrefix}${subtreeNode.originalRoutePath}`,
`${originalPathPrefix}${subtreeNode.originalRoutePath}`,
)
}
subtreeNode.filePath = `${node.directory}/${subtreeNode.filePath}`
Expand All @@ -208,11 +217,16 @@ export async function getRouteNodesRecursive(
case 'index': {
const { filePath, variableName, fullPath } = getFile(node.file)
const routePath = `${parentRoutePath}/`
const originalRoutePath = `${parentOriginalRoutePath}/`
return {
filePath,
fullPath,
variableName,
routePath,
originalRoutePath,
_routePathSegmentMetadata: parent?._routePathSegmentMetadata
? [...parent._routePathSegmentMetadata]
: undefined,
_fsRouteType: 'static',
_virtualParentRoutePath: virtualParentRoutePath,
} satisfies RouteNode
Expand Down
Loading
Loading