router: File-based routing doesn't support nested routes
Describe the bug
Not sure if this is a bug report or a feature request.
Nested routes don’t seem to work out of the box with file-based routing. The CLI uses the root as every route’s parent, regardless of directory structure.
Your Example Website or App
https://codesandbox.io/p/github/Swizec/tanstack-router-example/main
Steps to Reproduce the Bug or Issue
Run tsr generate and you get this config:
Object.assign(IndexRoute.options, {
path: "/",
getParentRoute: () => rootRoute,
});
Object.assign(StoryIdRoute.options, {
path: "/story/$id",
getParentRoute: () => rootRoute,
});
Expected behavior
Expected config based on file structure would be:
Object.assign(IndexRoute.options, {
path: "/",
getParentRoute: () => rootRoute,
});
Object.assign(StoryIdRoute.options, {
path: "/story/$id",
getParentRoute: () => IndexRoute,
});
Screenshots or Videos
No response
Platform
Platform independent, I think
Additional context
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 21 (6 by maintainers)
@tannerlinsley what was the lapse in documentation? I am seeing this where all my nested routes parents are being set to the rootRoute. Not sure if I have something configured incorrectly though.
@tannerlinsley The issue is still reproducable with the current version. Could you please reopen this issue?
Okay I think I figured out my issues so for anyone with similar routing issues. I misunderstood the filebased routing concepts. If you have a nested route (e.g.
/customers/$customerId- you need to have a top levelcustomerscomponent.This is what I originally had and was getting all my nested routes assigned a parentRoute of the root
customers/index.tsx
customers/$customerId.tsx
I was under the impression that the index.tsx in
/customersfolder would be set as the root for all routes in the customer directory. What you need is a top level component file with the same name as the nested route folder.EDIT:
Outside of finding the fix - I’m trying to make this work so all my files are contained in one directory and I don’t need a top level component file for my child roots to get the correct parent
This seems to outline a way to do that but my routeTree.gen file keeps updating my route to
/customers/index/routeif someone has found a way to do this where everything is contained in one directory let me know!