Skip to content

Commit 93138b0

Browse files
fix: Fix escaped underscore index route generation
1 parent 35a7d9c commit 93138b0

44 files changed

Lines changed: 988 additions & 13 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.changeset/bright-bats-fold.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/router-generator': patch
3+
---
4+
5+
Preserve escaped underscore segments when generating index routes across physical and virtual route trees, including pathless layouts, `physical()` prefixes, and `__virtual.ts` subtrees.

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,26 @@ export async function getRouteNodes(
128128
const filePath = replaceBackslash(
129129
path.join(normalizedDir, node.filePath),
130130
)
131-
const prefixPath = cleanPath(`/${normalizedDir}`)
132-
const routePath = cleanPath(`/${normalizedDir}${node.routePath}`)
131+
const { routePath: prefixPath, originalRoutePath: originalPrefixPath } =
132+
normalizedDir
133+
? determineInitialRoutePath(normalizedDir)
134+
: { routePath: '', originalRoutePath: '' }
135+
const routePath = cleanPath(`${prefixPath}${node.routePath}`)
133136

134137
node.variableName = routePathToVariable(
135-
cleanPath(`/${normalizedDir}/${removeExt(node.filePath)}`),
138+
cleanPath(`${prefixPath}/${removeExt(node.filePath)}`),
136139
)
137140
node._routePathSegmentMetadata = joinRoutePathSegmentMetadata(
138141
routePath,
139142
prefixPath,
140-
undefined,
143+
createRoutePathSegmentMetadata(prefixPath, originalPrefixPath),
141144
node._routePathSegmentMetadata,
142145
)
143146
node.routePath = routePath
144147
// Keep originalRoutePath aligned with routePath for escape detection
145148
if (node.originalRoutePath) {
146149
node.originalRoutePath = cleanPath(
147-
`/${normalizedDir}${node.originalRoutePath}`,
150+
`${originalPrefixPath}${node.originalRoutePath}`,
148151
)
149152
}
150153
node.filePath = filePath
@@ -313,10 +316,6 @@ export async function getRouteNodes(
313316
routePath = '/'
314317
}
315318

316-
if (lastOriginalSegment === updatedLastRouteSegment) {
317-
originalRoutePath = '/'
318-
}
319-
320319
// For layout routes, don't use '/' fallback - an empty path means
321320
// "layout for the parent path" which is important for physical() mounts
322321
// where route.tsx at root should have empty path, not '/'

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ export async function getRouteNodesRecursive(
152152
const children = await Promise.all(
153153
nodes.map(async (node) => {
154154
if (node.type === 'physical') {
155+
const {
156+
routePath: routePathPrefix,
157+
originalRoutePath: originalRoutePathPrefix,
158+
} = node.pathPrefix
159+
? determineInitialRoutePath(removeLeadingSlash(node.pathPrefix))
160+
: { routePath: '', originalRoutePath: '' }
155161
const { routeNodes, physicalDirectories } = await getRouteNodesPhysical(
156162
{
157163
...tsrConfig,
@@ -166,13 +172,16 @@ export async function getRouteNodesRecursive(
166172
)
167173
routeNodes.forEach((subtreeNode) => {
168174
const pathPrefix = cleanPath(
169-
`${parent?.routePath ?? ''}${node.pathPrefix}`,
175+
`${parent?.routePath ?? ''}${routePathPrefix}`,
176+
)
177+
const originalPathPrefix = cleanPath(
178+
`${parent?.originalRoutePath ?? parent?.routePath ?? ''}${originalRoutePathPrefix}`,
170179
)
171180
const literalPathPrefixSegments =
172181
createLiteralRoutePathSegmentMetadata(pathPrefix, parent, true)
173182
const routePath = cleanPath(`${pathPrefix}${subtreeNode.routePath}`)
174183
subtreeNode.variableName = routePathToVariable(
175-
`${node.pathPrefix}/${removeExt(subtreeNode.filePath)}`,
184+
`${routePathPrefix}/${removeExt(subtreeNode.filePath)}`,
176185
)
177186
subtreeNode._routePathSegmentMetadata = joinRoutePathSegmentMetadata(
178187
routePath,
@@ -184,7 +193,7 @@ export async function getRouteNodesRecursive(
184193
// Keep originalRoutePath aligned with routePath for escape detection
185194
if (subtreeNode.originalRoutePath) {
186195
subtreeNode.originalRoutePath = cleanPath(
187-
`${parent?.originalRoutePath ?? parent?.routePath ?? ''}${node.pathPrefix}${subtreeNode.originalRoutePath}`,
196+
`${originalPathPrefix}${subtreeNode.originalRoutePath}`,
188197
)
189198
}
190199
subtreeNode.filePath = `${node.directory}/${subtreeNode.filePath}`
@@ -208,11 +217,16 @@ export async function getRouteNodesRecursive(
208217
case 'index': {
209218
const { filePath, variableName, fullPath } = getFile(node.file)
210219
const routePath = `${parentRoutePath}/`
220+
const originalRoutePath = `${parentOriginalRoutePath}/`
211221
return {
212222
filePath,
213223
fullPath,
214224
variableName,
215225
routePath,
226+
originalRoutePath,
227+
_routePathSegmentMetadata: parent?._routePathSegmentMetadata
228+
? [...parent._routePathSegmentMetadata]
229+
: undefined,
216230
_fsRouteType: 'static',
217231
_virtualParentRoutePath: virtualParentRoutePath,
218232
} satisfies RouteNode

0 commit comments

Comments
 (0)