fix(router-generator): include Outlet in scaffolded route templates #7872
main fromrefs/pull/7872/head·opened Description#
Fixes #5351
When the route generator scaffolds a new route file, the generated component now includes <Outlet />.
That way if the route later becomes a layout route, child routes will actually render.
This matches what the root route template already does. For a leaf route <Outlet /> just renders nothing, so existing leaf routes are unaffected.
Applied to all three targets (React, Solid, Vue), following each target’s existing root-route style (React/Solid use a fragment, Vue mirrors its root template).
Implementation note#
The Outlet import is added as part of the default route template, not the shared tsrImports string.
tsrImports is also used by customScaffolding.routeTemplate and users can’t override it. Putting Outlet in there would force an unused import into every custom-scaffolded file that doesn’t use <Outlet />, triggering no-unused-vars / noUnusedLocals.
Keeping the import next to the default body that actually renders <Outlet /> avoids that problem. Default scaffolding gets the new behaviour, and tsrImports stays as the clean createFileRoute-only import that custom templates expect.
Context#
New users following the getting-started guide usually create files in this order: posts.tsx → posts/index.tsx → posts/$postId.tsx.
The auto-generated posts.tsx used to only contain <div>Hello "/posts"!</div> with no <Outlet />, so nested routes looked broken until the user manually added one.
Test plan#
-
pnpm nx run @tanstack/router-generator:test:unit— 119/119 pass (custom-scaffolding snapshots confirm no strayOutletimport) -
eslint ./srcclean -
tsc -p tsconfig.legacy.json— no type errors
AI assistance#
I used AI to help explore the generator internals and draft the code + snapshot updates.
I reviewed and validated every change myself (including confirming that tsrImports is shared with custom scaffolding) and can explain the reasoning during review.
Summary by CodeRabbit#
- New Features
- Generated non-lazy React, Solid, and Vue routes now include router outlet rendering.
- Nested route content can now appear within generated route components.
- Bug Fixes
- Updated generated route imports and output to consistently support outlet rendering across supported frameworks.
- 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/router-generator/tests/generator/custom-scaffolding/routes/index.tsx (1)
2-10: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick winInclude
<Outlet />in the component to match the new template contract.The
Outletcomponent is imported but unused in these custom scaffolding routes and snapshots. To align the custom scaffolding fixtures with the new default generator behavior (and to prevent unused-import warnings),RouteComponentshould render<Outlet />.packages/router-generator/tests/generator/custom-scaffolding/routes/index.tsx#L2-L10: UpdateRouteComponentto return a fragment containing the greeting and<Outlet />.packages/router-generator/tests/generator/custom-scaffolding/routes/api/bar.tsx#L2-L10: UpdateRouteComponentto return a fragment containing the greeting and<Outlet />.packages/router-generator/tests/generator/custom-scaffolding/snapshots/index.tsx#L2-L10: Update the snapshot'sRouteComponentto render<Outlet />.packages/router-generator/tests/generator/custom-scaffolding/snapshots/api/bar.tsx#L2-L10: Update the snapshot'sRouteComponentto render<Outlet />.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/router-generator/tests/generator/custom-scaffolding/routes/index.tsx` around lines 2 - 10, Update RouteComponent in packages/router-generator/tests/generator/custom-scaffolding/routes/index.tsx#L2-L10, packages/router-generator/tests/generator/custom-scaffolding/routes/api/bar.tsx#L2-L10, packages/router-generator/tests/generator/custom-scaffolding/snapshots/index.tsx#L2-L10, and packages/router-generator/tests/generator/custom-scaffolding/snapshots/api/bar.tsx#L2-L10 to return a fragment containing the existing greeting and the imported Outlet component, matching the new scaffolding template contract.🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. Outside diff comments: In `@packages/router-generator/tests/generator/custom-scaffolding/routes/index.tsx`: - Around line 2-10: Update RouteComponent in packages/router-generator/tests/generator/custom-scaffolding/routes/index.tsx#L2-L10, packages/router-generator/tests/generator/custom-scaffolding/routes/api/bar.tsx#L2-L10, packages/router-generator/tests/generator/custom-scaffolding/snapshots/index.tsx#L2-L10, and packages/router-generator/tests/generator/custom-scaffolding/snapshots/api/bar.tsx#L2-L10 to return a fragment containing the existing greeting and the imported Outlet component, matching the new scaffolding template contract.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
b0c3eac5-77aa-4c70-ae27-b69a3835b329📥 Commits
Reviewing files that changed from the base of the PR and between edf55759d8e9e5e7954945fdda2ee81c48e8c713 and 1e026f4900a73b8f43f5e08ee646cbb62561c441.
📒 Files selected for processing (6)
packages/router-generator/src/template.tspackages/router-generator/tests/generator/custom-scaffolding/routes/api/bar.tsxpackages/router-generator/tests/generator/custom-scaffolding/routes/index.tsxpackages/router-generator/tests/generator/custom-scaffolding/snapshots/api/bar.tsxpackages/router-generator/tests/generator/custom-scaffolding/snapshots/index.tsxpackages/router-generator/tests/generator/file-modification/snapshots/initiallyEmpty.tsx
WuP1ao0(GitHub profile, opens in a new tab) linked this to fix #5351
Sign in to join this conversation.
📝 Walkthrough
Walkthrough#
Non-lazy React, Solid, and Vue route templates now generate outlet rendering alongside greeting content. The React generator snapshot is updated to match the new outlet-containing route output.
Changes#
Layout route outlet generation
packages/router-generator/src/template.tsOutletalongside the generated greeting content.packages/router-generator/tests/generator/file-modification/snapshots/initiallyEmpty.tsxEstimated code review effort: 3 (Moderate) | ~20 minutes
Possibly related PRs
Outletrendering for non-lazy nested routes.Suggested labels:
package: router-generator🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
#5351by adding Outlet to generated layout-route templates for React, Solid, and Vue.✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share