router: Context is undefined after redirect
Describe the bug
I use authentication, similar to Authenticated Routes example from documentation. So, when user is on /$lang page, if user is not authorised, user is been redirecting to /$lang/login page. It happens, but useRouteContext returns undefined instead of context. I set context in RouterProvider like this. If I reload the page, issue disappears
<RouterProvider
router={router}
context={{ auth, queryClient, showAppAlert }}
/>
I tested on 1.29.2, 1.30.1, 1.31.1, issue happens. I also tested on 1.26.21 and issue does not happen. I didn’t test versions between 1.26.21 and 1.29.1
Your Example Website or App
Steps to Reproduce the Bug or Issue
It happens in our live admin panel(admin panel at all is in development, but some functionality have already been implemented)
Expected behavior
Context, if it set in router provider is always available and not undefined
Screenshots or Videos
https://github.com/TanStack/router/assets/57814533/5d067867-024a-4e8b-9891-ecfd332771f7
Platform
- Version: 1.29.2-1.31.1
Additional context
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 47 (22 by maintainers)
I’m so sorry to say this, but it still doesn’t work for me with 1.32.11 😦 This is the bug from hell, eh?
@SeanCassiere Managed to repro: https://stackblitz.com/edit/github-dxj2gr-btjqhp
I found another edge case in
v1.34.9that causescontextto becomeundefinedif the route that throws the redirect has search params that are being validated throughvalidateSearch, and the validation fails/throws (e.g. withzod).Here’s a complete repro: https://stackblitz.com/edit/tanstack-router-qbczsv?file=src%2Froutes%2Findex.tsx
@callumbooth this problem has been fixed with the 1.32.16 release.
@maksymskuibida please retest with 1.32.16 using
throw redirect()instead ofnavigate. If this doesn’t work for you, we’ll need a fresh minimal reproduction of your setup (without any unnecessary setup like auth, components, logging, etc.).@cpakken Your issue will tracked separately in #1618. This issue is already a bit too crowded.
1.32.11 fixes my issue/reproduction. Cheers!
This should now be fixed with the 1.32.11 release.
📢 Huge shoutout to @freshgiammi for the fix.
@freshgiammi this seems to do the trick.
I didn’t have time to make a reproduction but this is exactly what I said I’m facing in https://github.com/TanStack/router/issues/1531#issuecomment-2096533191.
An error is thrown here on the offending route (in this case,
/),and then the cached match in the router store gets updated (here). However, the route remains in a pending state because the finally statement overrides this by using the scopedmatchobject. On the next iteration, it is then skipped as it’s marked withisFetching: true.Moving the
updateMatchcall from the finally block to the end of the try block fixes this context issue for me, and so does reverting this commit which was introduced in 1.28.6.cc @SeanCassiere I don’t know what that commit was supposed to fix, so I haven’t made a PR reverting it but I’m happy to if needed 👍
I tested replacing throw redirect({}) with router.navigate() and now issue does not appear. So, issue appear only with throw redirect(). I event don’t need setTimeout, it works without it( I do it in beforeLoad, not in component, so hook is not available )
As promised, here’s a new issue with reproduction: https://github.com/TanStack/router/issues/1637
Unfortunately this isn’t working for me in
1.32.11.I’ve managed to create a repro in stackblitz: https://stackblitz.com/edit/github-kx4dql-mgkaub?file=src%2Froutes%2Fabout.tsx
I think I’ve narrowed it down to something related to
<StrictMode>.Repro steps:
/aboutNow if you remove StrictMode and retest the issue doesn’t happen.
I’ve tried taking a stab at it since I’ve been facing this issue for a bit as well, but I’m hitting a wall. My issue is a bit different from the ones shared in the reproductions (can’t share my code, and I haven’t managed yet to create a repro), but basically context within loader (not beforeLoad, it’s available there) is undefined after a redirect. My routes are like this:
The unauthorized page redirects users from within a
beforeLoadto the index page, but that causes the context in the index’s loader to be undefined. What I’ve seen is that once a redirect is executed, the first three routes are cached and retrieved from the Router’s store, the and the unauthorized page is swapped with the index page with an empty context (since they both share the same parent tree). Now, apparently all routes are created with no context (here) and get their context from here, however the newly matched route (Index, where the user is being redirected) doesn’t reach that code block and consistently halts after executing https://github.com/TanStack/router/blob/main/packages/react-router/src/router.ts#L1563. It looks like the flow just gets interrupted after the beforeLoad is executed, with the Router then failing here as no context can be provided to the loader for the last route matched (because it never gets to merge the context with the matched route).I’ll try making a repro again, but in the meanwhile I hope this can help pinpoint the issue somehow 🤷♂️