Skip to content
Source
Sign inSign up
TanStack/router

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

Send to my agent
Closedcpakken(GitHub profile, opens in a new tab) opened this issue 4 comments

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:

  • the routes of the _layout returns the context correctly
  • the root router's context has the returns context correctly
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

  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Version: [e.g. 91.1]

Additional context

No response

  1. @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. ...
  2. SeanCassiere added labelsinformation needed

  3. @SeanCassiere @tannerlinsley just released v1.32.16 30 minutes ago and fixes the issue. Thanks so much! ♥

Sign in to join this conversation.