TanStack / router

πŸ€– Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering.
https://tanstack.com/router
MIT License
7.27k stars 495 forks source link

Layout Route Contexts NOT being set on non-initial redirects in beforeLoad() #1618

Closed cpakken closed 2 weeks ago

cpakken commented 2 weeks ago

Describe the bug

Layout Route Contexts NOT being set on non-initial redirects in beforeLoad()

Repo reproduction https://github.com/cpakken/issue-tanstack-router-layout-route-context "@tanstack/react-router": "^1.32.13",

On non-initial loads, when a _layout route's beforeLoads() is triggered by navigation, router.invalidate(), the route's contex returns undefined

Please run vite dev and use Auth Dev Controls to toggle auth state changes and observe the _LAYOUT/context returns undefined

Note:

export const Route = createFileRoute('/_not_authed')({
  beforeLoad: ({ context: { auth } }) => {
    //If the user is already authenticated, redirect them to the main dashboard
    if (auth.get()) {
      console.log('redirecting to /')
      throw redirect({ to: '/' })
    }
  },
  component: memo(Component),
})

function Component() {
  const context = Route.useRouteContext() //this returns undefined on non-initial redirect
  const rootContext = useRouteContext({ from: '__root__' }) //this seems to resolve
  console.log('_not_authed/context:', context, rootContext)

  return (
    <div className="bg-gray-300">
    ...
    </div>
  )
}

No context in _auth

image

root has context image

└── πŸ“routes
    └── πŸ“_authed 
        └── index.lazy.tsx
        └── user-settings.lazy.tsx
    └── _authed.tsx
    └── πŸ“_not_authed
        └── login.lazy.tsx
        └── signup.lazy.tsx
    └── _not_authed.tsx
    └── __root.tsx

Your Example Website or App

https://github.com/cpakken/issue-tanstack-router-layout-route-context

Steps to Reproduce the Bug or Issue

See readme.

Expected behavior

Route.useRouteContext() should return the context instead of undefined on non-initial redirects

Screenshots or Videos

No response

Platform

Additional context

No response

cpakken commented 2 weeks ago

Maybe same error as https://github.com/TanStack/router/issues/1604

cpakken commented 2 weeks ago

Also: https://github.com/TanStack/router/issues/1531

SeanCassiere commented 2 weeks ago

@cpakken I tried out your setup on 1.32.16 and either the issue has been fixed or I'm simply not seeing the issue.

https://github.com/TanStack/router/assets/33615041/c40cfca2-e9d8-4aea-8920-578b6b932c72

Screenshot 2024-05-18 at 17 11 10

https://stackblitz.com/edit/github-5eojgx?file=src%2Fmain.tsx

If the issue still persists, you'll need to detail the exact steps needed to be taken to recreate scenario.

Eg:

  1. Go to ...
  2. When on ... reload.
  3. ...
cpakken commented 2 weeks ago

@SeanCassiere @tannerlinsley just released v1.32.16 30 minutes ago and fixes the issue. Thanks so much! β™₯