Repository navigation
fix(router-generator): type route-group layouts with full path '/'
#8857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8d2828f
fix(router-generator): type route-group layouts with full path `'/'`
Sheraff 8758575
fix(router-generator): a childless route-group layout next to the ind…
Sheraff e5a9b69
fix(router-generator): a pathless layout that carries a path still co…
Sheraff 23fd9c4
fix(router-generator): keep main's full-path conflicts, change only t…
Sheraff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
packages/router-generator/tests/childless-pathless-layout.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`) | ||
| }) | ||
| }) | ||
|
|
||
| // 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".', | ||
| ) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.