fix(router-generator): include Outlet in scaffolded route templates#7872
fix(router-generator): include Outlet in scaffolded route templates#7872WuP1ao0 wants to merge 2 commits into
Conversation
When the route generator scaffolds a new route file, the generated component now includes <Outlet /> so child routes render correctly if the route later becomes a layout route. This matches the existing root route template behavior and prevents new users from getting stuck when following the getting-started guide with nested routes. Fixes TanStack#5351
📝 WalkthroughWalkthroughNon-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. ChangesLayout route outlet generation
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested labels: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ 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. Comment |
There was a problem hiding this comment.
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
📒 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
…avoid unused import in custom scaffolding (TanStack#5351)
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
Outletimport is added as part of the default route template, not the sharedtsrImportsstring.tsrImportsis also used bycustomScaffolding.routeTemplateand users can’t override it. PuttingOutletin there would force an unused import into every custom-scaffolded file that doesn’t use<Outlet />, triggeringno-unused-vars/noUnusedLocals.Keeping the import next to the default body that actually renders
<Outlet />avoids that problem. Default scaffolding gets the new behaviour, andtsrImportsstays as the cleancreateFileRoute-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.tsxused 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 ./srccleantsc -p tsconfig.legacy.json— no type errorsAI 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
tsrImportsis shared with custom scaffolding) and can explain the reasoning during review.Summary by CodeRabbit