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

Loader context is undefined after redirect #1594

Closed KirillTregubov closed 2 weeks ago

KirillTregubov commented 2 weeks ago

Describe the bug

If you throw a redirect in the beforeLoad of page A, then navigate back to page A (without the redirect being thrown), loader receives an undefined context.

I discovered this because I am working on a project where the client needs to complete a setup step. If they navigate to /page, I fetch the setup status in the beforeLoad and redirect them to /setup if required. I noticed that when the user is redirected, if I try to navigate back to /page after setup is complete, then the page's loader context is undefined.

Your Example Website or App

https://stackblitz.com/edit/github-xhruej?file=src%2Froutes%2Fabout.tsx,src%2Froutes%2Findex.tsx&file=src%2Froutes%2Fabout.tsx

Steps to Reproduce the Bug or Issue

  1. Open the DevTools console.
  2. Notice you are on the About page. If you look in the console, you were redirected on initial load.
  3. Press the "Navigate" button.
  4. Notice you are on the Index page. If you look in the console, you will see that the loader context is undefined.

Expected behavior

  1. Open the DevTools console.
  2. Notice you are on the About page. If you look in the console, you were redirected on initial load.
  3. Press the "Navigate" button.
  4. Notice you are on the Index page. If you look in the console, the loader context is an object containing the queryClient

After navigating to a page that has been redirected from, the loader context should not be undefined. We are suggested by the docs / examples to use loader: ({ context: { queryClient }) => {} when defining loaders, which is not valid if context can be undefined.

Screenshots or Videos

No response

Platform

Additional context

No response