Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layout in folder doesn't work #1150

Closed
WailRoth opened this issue Feb 5, 2024 · 9 comments · Fixed by #1272
Closed

Layout in folder doesn't work #1150

WailRoth opened this issue Feb 5, 2024 · 9 comments · Fixed by #1272

Comments

@WailRoth
Copy link

WailRoth commented Feb 5, 2024

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

@SeanCassiere
Copy link
Member

SeanCassiere commented Feb 5, 2024

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.

@ferretwithaberet
Copy link

ferretwithaberet commented Mar 1, 2024

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].

@divmgl
Copy link

divmgl commented Mar 5, 2024

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`} />
}

@brandonryan
Copy link

brandonryan commented Mar 8, 2024

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
Copy link

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

@schiller-manuel
Copy link
Contributor

can you please create a new issue?

@mwood23
Copy link

mwood23 commented Mar 9, 2024

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

@alicantorun
Copy link

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

@schiller-manuel
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants