router icon indicating copy to clipboard operation
router copied to clipboard

Layout in folder doesn't work

Open WailRoth opened this issue 1 year ago • 1 comments

Describe the bug

Currently this works /_layout/login.tsx -> /login auth._layout.login.tsx -> /auth/login

Its just that this doesn't work /auth/_layout/login.tsx -> /auth/layout/login

As you can see on the reproduction link, the _layout in the /auth/ folder is totally ignored by the router, ignoring my layout.

Your Example Website or App

https://stackblitz.com/~/edit/github-odjbcq-wjavnw?file=src%2Froutes%2F__root.tsx

Steps to Reproduce the Bug or Issue

Go on /auth/login, the layout isn't applied.

Expected behavior

Apply the layout on /auth/_layout pages

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: [Edge]
  • Version 114.0.1823.82 (Version officielle) (arm64)

Additional context

No response

WailRoth avatar Feb 05 '24 08:02 WailRoth

For more context, this bug report is part of this Discord thread.

The problem is that the router-cli (vite-plugin) incorrectly infers the routing structure of a layout route when it is nested in a parent route.

✅ When the layout route is defined at the root, it generates the route tree correctly. Example

src/routes
  _layout.tsx
  _layout/auth.login.tsx // generates the route /auth/login
  _layout/auth.register.tsx // generates the route /auth/register
  ...

✅ It also works correctly when flat files are used.

src/routes
  auth._layout.tsx
  auth._layout.login.tsx // generates the route /auth/login
  auth._layout.register.tsx // generates the route /auth/register
  ...

❌ However, when the layout is nested inside a parent folder is when stuff starts to break. Reproduction where stuff is broken

src/routes
  auth/
    _layout.tsx
    _layout/login.tsx // generates the route /auth/layout/login
    _layout/register.tsx // generates the route /auth/layout/register
  ...

✅ Weirdly enough, it works correctly if you define a route.tsx and index.tsx file within the auth route. Example

src/routes
  auth/
    route.tsx
    index.tsx
    _layout.tsx
    _layout/login.tsx // generates the route /auth/login
    _layout/register.tsx // generates the route /auth/register
  ...

🐞 My best guess is that this looks to be a bug with both the detection of layout routes and how file-routes get attached to the virtual routes.

SeanCassiere avatar Feb 05 '24 08:02 SeanCassiere

I have the same issue, this is my folder structure: image I would expect _BankAccounts to not be added to the URL while being used as a layout for everything under _BankAccounts/. What actually happens is that /BankAccounts gets added to the URL.

The generated code from routeTree.gen.ts looks like this:

const ErpCompanyIdSettingsBankAccountsBankAccountsRoute =
  ErpCompanyIdSettingsBankAccountsBankAccountsImport.update({
    path: '/settings/bank-accounts/BankAccounts',
    getParentRoute: () => ErpCompanyIdRoute,
  } as any).lazy(() =>
    import(
      './routes/erp/$companyId/settings/bank-accounts/_BankAccounts.lazy'
    ).then((d) => d.Route),
  )

Edit:

✅ Weirdly enough, it works correctly if you define a route.tsx and index.tsx file within the auth route. Example

src/routes
  auth/
    route.tsx
    index.tsx
    _layout.tsx
    _layout/login.tsx // generates the route /auth/login
    _layout/register.tsx // generates the route /auth/register
  ...

This does not seem to work in my case, I am using @tanstack/[email protected].

ferretwithaberet avatar Mar 01 '24 11:03 ferretwithaberet

Having this issue too. Nested layout routes are not working.

Screenshot 2024-03-05 at 9 19 15 AM

Projects and settings are inaccessible unless I move them out of the route. Pretty serious bug....... basically blocks development on this project.

The use case is that a protected route named _authenticated.workspace.$workspaceId should always redirect to _authenticated.workspace.$workspaceId.projects, but all inner routes need layouts too. So we need to be able to create a non-nested route _authenticated.workspace.$workspaceId_ that does the redirect, and a layout route _authenticated.workspace.$workspaceId._layout.

This doesn't work at all.

Edit: we figured out a way to get what we wanted but it's seriously convoluted. We now have a _authenticated.workspace.$workspaceId route that is both an <Outlet /> but also conditional redirect that checks pathname:

const { pathname } = useLocation()

workspaceId ??= activeWorkspaceId ?? Array.from(workspaceIds)[0]

if (pathname!.split("/").filter(Boolean).length <= 2 && workspaceId) {
  return <Navigate to={`/workspace/${workspaceId}/projects`} />
}

divmgl avatar Mar 05 '24 14:03 divmgl

Dev-tools needs an update. Gets confused about nested index routes:

Router Dev Tools: image File Tree: image

Thank you for this btw! Was the only thing holding me back from using this library for our production project.

brandonryan avatar Mar 08 '24 17:03 brandonryan

I think it might actually be a bug with the route id? image

brandonryan avatar Mar 08 '24 17:03 brandonryan

can you please create a new issue?

schiller-manuel avatar Mar 08 '24 18:03 schiller-manuel

I think this is still broken? The tree gen isn't picking up on the _ in workspaceLayout

CleanShot 2024-03-09 at 18 03 16@2x CleanShot 2024-03-09 at 18 02 59@2x

mwood23 avatar Mar 09 '24 23:03 mwood23

Is this issue really fixed? Still only works for me using the following structure recommended here, which feels hacky

Screenshot 2024-03-11 at 20 56 41

alicantorun avatar Mar 11 '24 19:03 alicantorun

@alicantorun can you please provide a minimal example by forking one of the examples on stackblitz ? please also explain which workaround you consider "hacky" and would like to avoid.

schiller-manuel avatar Mar 11 '24 20:03 schiller-manuel