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)

Most upvoted comments

After speaking with @Swizec, this was just a lapse in documentation. Thanks Swi!

@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?

@tannerlinsley any follow up on this? I am running into issues now with creating breadcrumbs as all my nested routes are getting their parent set to the rootRoute so my breadcrumbs aren’t accurate.

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 level customers component.

This is what I originally had and was getting all my nested routes assigned a parentRoute of the root

__root.tsx
/customers (folder)
     index.tsx
     $customerId.tsx

customers/index.tsx

export const Route = new FileRoute('/customers').createRoute({
  component: CustomersPage,
});

customers/$customerId.tsx

export const Route = new FileRoute('/customers/$customerId').createRoute({
  component: CustomerPage,
})

I was under the impression that the index.tsx in /customers folder 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.

__root.tsx
customers.tsx (THIS IS NEEDED)
/customers (folder)
     index.tsx
     $customerId.tsx

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

image

This seems to outline a way to do that but my routeTree.gen file keeps updating my route to /customers/index/route if someone has found a way to do this where everything is contained in one directory let me know!