Skip to content

Commit

Permalink
fix: generator should not infer trailing slash is not necessary for i…
Browse files Browse the repository at this point in the history
…ndex routes (#1597)
  • Loading branch information
chorobin committed May 16, 2024
1 parent f56c234 commit a770c08
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
27 changes: 22 additions & 5 deletions packages/router-generator/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,11 @@ export async function generator(config: Config) {
.map((routeNode) => {
const filePathId = removeTrailingUnderscores(routeNode.routePath)
const id = removeGroups(filePathId ?? '')
const fullPath = removeGroups(
removeUnderscores(removeLayoutSegments(routeNode.routePath)) ?? '',
)
return `'${filePathId}': {
id: '${id}'
path: '${routeNode.cleanedPath}'
fullPath: '${fullPath}'
path: '${inferPath(routeNode)}'
fullPath: '${inferFullPath(routeNode)}'
preLoaderRoute: typeof ${routeNode.variableName}Import
parentRoute: typeof ${
routeNode.isVirtualParentRequired
Expand Down Expand Up @@ -795,3 +792,23 @@ export function hasParentRoute(

return hasParentRoute(routes, node, parentRoutePath)
}

/**
* Infers the full path for use by TS
*/
export const inferFullPath = (routeNode: RouteNode): string => {
const fullPath = removeGroups(
removeUnderscores(removeLayoutSegments(routeNode.routePath)) ?? '',
)

return routeNode.cleanedPath === '/' ? fullPath : fullPath.replace(/\/$/, '')
}

/**
* Infers the path for use by TS
*/
export const inferPath = (routeNode: RouteNode): string => {
return routeNode.cleanedPath === '/'
? routeNode.cleanedPath
: routeNode.cleanedPath?.replace(/\/$/, '') ?? ''
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ declare module '@tanstack/react-router' {
}
'/blog/$slug/': {
id: '/blog/$slug/'
path: '/$slug/'
fullPath: '/blog/$slug/'
path: '/$slug'
fullPath: '/blog/$slug'
preLoaderRoute: typeof BlogSlugIndexImport
parentRoute: typeof BlogRouteImport
}
'/posts/$postId/': {
id: '/posts/$postId/'
path: '/$postId/'
fullPath: '/posts/$postId/'
path: '/$postId'
fullPath: '/posts/$postId'
preLoaderRoute: typeof PostsPostIdIndexImport
parentRoute: typeof PostsRouteImport
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ declare module '@tanstack/react-router' {
}
'/posts/$postId/': {
id: '/posts/$postId/'
path: '/$postId/'
fullPath: '/posts/$postId/'
path: '/$postId'
fullPath: '/posts/$postId'
preLoaderRoute: typeof PostsPostIdIndexImport
parentRoute: typeof PostsRouteImport
}
Expand Down

0 comments on commit a770c08

Please sign in to comment.