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/route-group-layout-full-path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/router-generator': patch
---

A pathless layout inside a route group (`routes/(group)/_layout.tsx`) or a route group's own layout (`routes/(group)/route.tsx`) is typed with the full path `'/'` in `FileRoutesByPath`, like the router gives it at runtime, instead of `''`. When no index route claims `/`, the layout also appears under `'/'` in `FileRoutesByFullPath` and `FileRoutesByTo`, as a pathless layout outside a group already does.
2 changes: 1 addition & 1 deletion e2e/react-router/basic-file-based/src/routeTree.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1711,7 +1711,7 @@ declare module '@tanstack/react-router' {
'/(group)/_layout': {
id: '/(group)/_layout'
path: ''
fullPath: ''
fullPath: '/'
preLoaderRoute: typeof groupLayoutRouteImport
parentRoute: typeof rootRouteImport
}
Expand Down
2 changes: 1 addition & 1 deletion e2e/solid-router/basic-file-based/src/routeTree.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ declare module '@tanstack/solid-router' {
'/(group)/_layout': {
id: '/(group)/_layout'
path: ''
fullPath: ''
fullPath: '/'
preLoaderRoute: typeof groupLayoutRouteImport
parentRoute: typeof rootRouteImport
}
Expand Down
2 changes: 1 addition & 1 deletion e2e/vue-router/basic-file-based-jsx/src/routeTree.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ declare module '@tanstack/vue-router' {
'/(group)/_layout': {
id: '/(group)/_layout'
path: ''
fullPath: ''
fullPath: '/'
preLoaderRoute: typeof groupLayoutRouteImport
parentRoute: typeof rootRouteImport
}
Expand Down
2 changes: 1 addition & 1 deletion e2e/vue-router/basic-file-based-sfc/src/routeTree.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ declare module '@tanstack/vue-router' {
'/(group)/_layout': {
id: '/(group)/_layout'
path: ''
fullPath: ''
fullPath: '/'
preLoaderRoute: typeof groupLayoutRouteImport
parentRoute: typeof rootRouteImport
}
Expand Down
2 changes: 1 addition & 1 deletion examples/vue/basic-file-based-jsx/src/routeTree.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ declare module '@tanstack/vue-router' {
'/(group)/_layout': {
id: '/(group)/_layout'
path: ''
fullPath: ''
fullPath: '/'
preLoaderRoute: typeof groupLayoutRouteImport
parentRoute: typeof rootRouteImport
}
Expand Down
2 changes: 1 addition & 1 deletion examples/vue/basic-file-based-sfc/src/routeTree.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ declare module '@tanstack/vue-router' {
'/(group)/_layout': {
id: '/(group)/_layout'
path: ''
fullPath: ''
fullPath: '/'
preLoaderRoute: typeof groupLayoutRouteImport
parentRoute: typeof rootRouteImport
}
Expand Down
18 changes: 15 additions & 3 deletions packages/router-generator/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -924,9 +924,10 @@ const inferPath = (routeNode: RouteNode): string => {
}

/**
* Infers the full path for use by TS
* The full path with groups and pathless segments removed. A route group's
* layout (e.g. `/(group)/_layout`) strips down to `''`.
*/
export const inferFullPath = (routeNode: RouteNode): string => {
const stripFullPath = (routeNode: RouteNode): string => {
const fullPath = removeGroups(
removeLayoutSegmentsAndUnderscoresWithEscape(
routeNode.routePath,
Expand All @@ -949,6 +950,13 @@ export const inferFullPath = (routeNode: RouteNode): string => {
return fullPath.replace(/\/$/, '')
}

/**
* Infers the full path for use by TS
*/
export const inferFullPath = (routeNode: RouteNode): string =>
// A route group's layout has the router's full path `/`
stripFullPath(routeNode) || '/'

const shouldPreferIndexRoute = (
current: RouteNode,
existing: RouteNode,
Expand Down Expand Up @@ -1112,8 +1120,12 @@ Conflicting files: \n ${emptyPathRoutes
throw new Error(errorMessage)
}

// A route group's layout keeps its stripped path `''` here, so one without
// child routes yet doesn't conflict with the index route. A childless
// `_auth.tsx` does: the route tree lists the index route first, and the
// router would then match `/` to the layout
const routes = _routes.map((d) => {
const inferredFullPath = inferFullPath(d)
const inferredFullPath = stripFullPath(d)
return { ...d, inferredFullPath }
})

Expand Down
107 changes: 107 additions & 0 deletions packages/router-generator/tests/childless-pathless-layout.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import fs from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import { afterEach, describe, expect, it, test } from 'vitest'

import { Generator, getConfig } from '../src'

let tempDir: string | undefined

afterEach(async () => {
if (tempDir) {
await fs.rm(tempDir, { recursive: true, force: true })
tempDir = undefined
}
})

function routeSource(id: string) {
if (id === '__root__') {
return `import { createRootRoute } from '@tanstack/react-router'

export const Route = createRootRoute()
`
}
return `import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('${id}')()
`
}

async function generate(files: Record<string, string>) {
tempDir = await fs.mkdtemp(
path.join(os.tmpdir(), 'tsr-generator-childless-layout-'),
)
const routesDirectory = path.join(tempDir, 'routes')
for (const [file, id] of Object.entries(files)) {
const filePath = path.join(routesDirectory, file)
await fs.mkdir(path.dirname(filePath), { recursive: true })
await fs.writeFile(filePath, routeSource(id))
}
const generatedRouteTree = path.join(tempDir, 'routeTree.gen.ts')
const generator = new Generator({
config: getConfig(
{
disableLogging: true,
routesDirectory,
generatedRouteTree,
tmpDir: path.join(tempDir, 'tmp'),
},
tempDir,
),
root: tempDir,
})
await generator.run()
return fs.readFile(generatedRouteTree, 'utf-8')
}

// A route group's layout without child routes yet next to the index route
// generates, as before route-group layouts were typed with the full path `/`.
describe('a route-group layout without child routes yet next to the index route', () => {
it.each([
{ file: '(group)/_layout.tsx', id: '/(group)/_layout' },
{ file: '(marketing)/route.tsx', id: '/(marketing)' },
])('$file is added to the route tree', async ({ file, id }) => {
const routeTree = await generate({
'__root.tsx': '__root__',
'index.tsx': '/',
[file]: id,
})

expect(routeTree).toContain(`'${id}': typeof`)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
})
})

// The route tree lists the index route first, and the router then matches
// `/` to a pathless layout without child routes instead of the index route.
// The generator refuses that pair rather than hide the index route.
test('a pathless layout without child routes yet conflicts with the index route', async () => {
await expect(
generate({
'__root.tsx': '__root__',
'index.tsx': '/',
'_auth.tsx': '/_auth',
}),
).rejects.toThrow(
'Conflicting configuration paths were found for the following routes: "/", "/".',
)
})

// A pathless layout whose parent directory has no route file of its own
// (`foo/_layout.tsx` without `foo.tsx`) carries that directory's path, so it
// matches `/foo` like any other route there.
describe('a layout without child routes yet that adds a path', () => {
it.each([
{ file: '(group)/foo.tsx', id: '/(group)/foo' },
{ file: 'foo_.tsx', id: '/foo_' },
])('conflicts with $file at the same full path', async ({ file, id }) => {
await expect(
generate({
'__root.tsx': '__root__',
'foo/_layout.tsx': '/foo/_layout',
[file]: id,
}),
).rejects.toThrow(
'Conflicting configuration paths were found for the following routes: "/foo", "/foo".',
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ const groupCLayoutCIndexRoute = groupCLayoutCIndexRouteImport.update({
} as any)

export interface FileRoutesByFullPath {
'/': typeof groupCLayoutCIndexRoute
'/login': typeof groupALayoutALoginRoute
'/signup': typeof groupALayoutASignupRoute
'/dashboard': typeof groupBLayoutBDashboardRoute
'/': typeof groupCLayoutCIndexRoute
}
export interface FileRoutesByTo {
'/': typeof groupCLayoutCIndexRoute
'/login': typeof groupALayoutALoginRoute
'/signup': typeof groupALayoutASignupRoute
'/dashboard': typeof groupBLayoutBDashboardRoute
'/': typeof groupCLayoutCIndexRoute
}
export interface FileRoutesById {
__root__: typeof rootRouteImport
Expand All @@ -74,9 +74,9 @@ export interface FileRoutesById {
}
export interface FileRouteTypes {
fileRoutesByFullPath: FileRoutesByFullPath
fullPaths: '/login' | '/signup' | '/dashboard' | '/'
fullPaths: '/' | '/login' | '/signup' | '/dashboard'
fileRoutesByTo: FileRoutesByTo
to: '/login' | '/signup' | '/dashboard' | '/'
to: '/' | '/login' | '/signup' | '/dashboard'
id:
| '__root__'
| '/(group-a)/_layout-a'
Expand All @@ -99,21 +99,21 @@ declare module '@tanstack/react-router' {
'/(group-a)/_layout-a': {
id: '/(group-a)/_layout-a'
path: ''
fullPath: ''
fullPath: '/'
preLoaderRoute: typeof groupALayoutARouteImport
parentRoute: typeof rootRouteImport
}
'/(group-b)/_layout-b': {
id: '/(group-b)/_layout-b'
path: ''
fullPath: ''
fullPath: '/'
preLoaderRoute: typeof groupBLayoutBRouteImport
parentRoute: typeof rootRouteImport
}
'/(group-c)/_layout-c': {
id: '/(group-c)/_layout-c'
path: ''
fullPath: ''
fullPath: '/'
preLoaderRoute: typeof groupCLayoutCRouteImport
parentRoute: typeof rootRouteImport
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const fooAsdfbarLayoutXyzLazyRoute = fooAsdfbarLayoutXyzLazyRouteImport
)

export interface FileRoutesByFullPath {
'/': typeof barBarRouteWithChildren
'/hello': typeof barBarHelloRoute
'/asdf': typeof fooAsdfanotherGroupLayoutRouteWithChildren
'/asdf/$id': typeof fooAsdfbarIdRoute
Expand All @@ -85,6 +86,7 @@ export interface FileRoutesByFullPath {
'/asdf/xyz': typeof fooAsdfbarLayoutXyzLazyRoute
}
export interface FileRoutesByTo {
'/': typeof barBarRouteWithChildren
'/hello': typeof barBarHelloRoute
'/asdf': typeof fooAsdfanotherGroupLayoutRouteWithChildren
'/asdf/$id': typeof fooAsdfbarIdRoute
Expand All @@ -108,6 +110,7 @@ export interface FileRoutesById {
export interface FileRouteTypes {
fileRoutesByFullPath: FileRoutesByFullPath
fullPaths:
| '/'
| '/hello'
| '/asdf'
| '/asdf/$id'
Expand All @@ -117,6 +120,7 @@ export interface FileRouteTypes {
| '/asdf/xyz'
fileRoutesByTo: FileRoutesByTo
to:
| '/'
| '/hello'
| '/asdf'
| '/asdf/$id'
Expand Down Expand Up @@ -151,7 +155,7 @@ declare module '@tanstack/react-router' {
'/(bar)/_bar': {
id: '/(bar)/_bar'
path: ''
fullPath: ''
fullPath: '/'
preLoaderRoute: typeof barBarRouteImport
parentRoute: typeof rootRouteImport
}
Expand Down
Loading