Giter Site home page Giter Site logo

Layout in folder doesn't work about router HOT 9 CLOSED

WailRoth avatar WailRoth commented on June 10, 2024 1
Layout in folder doesn't work

from router.

Comments (9)

SeanCassiere avatar SeanCassiere commented on June 10, 2024 18

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.

from router.

ferretwithaberet avatar ferretwithaberet commented on June 10, 2024 2

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

from router.

alicantorun avatar alicantorun commented on June 10, 2024 2

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

from router.

schiller-manuel avatar schiller-manuel commented on June 10, 2024 1

can you please create a new issue?

from router.

divmgl avatar divmgl commented on June 10, 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`} />
}

from router.

brandonryan avatar brandonryan commented on June 10, 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.

from router.

brandonryan avatar brandonryan commented on June 10, 2024

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

from router.

mwood23 avatar mwood23 commented on June 10, 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

from router.

schiller-manuel avatar schiller-manuel commented on June 10, 2024

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

from router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.