Undefined context after redirect #1670
Send to my agentDescribe the bug#
Seems like the issue with undefined context is not fully resolved yet. When redirecting I'm seeing undefined context.
Some criteria to trigger the issue
defaultPreload: 'intent'has to be set in the router- The
beforeLoadfunction in the layout that provides the context return a promise - The redirect is triggered in a loader fn after a delay. 500ms seems to always trigger the issue, 100ms does not trigger the issue. I assume it has to be long enough so you click the link before the preload has finished.
Your Example Website or App#
https://stackblitz.com/edit/github-xjvg3f?file=src%2Froutes%2Fabout.tsx
Steps to Reproduce the Bug or Issue#
- Open repro project in stackblitz
- Click the
Click me to crashlink - Observe crash caused by missing context
Expected behavior#
I expect the context to be present after redirect
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#
These issues are related, but I'm not certain if they are exactly the same so I created a new issue. https://github.com/TanStack/router/issues/1656 https://github.com/TanStack/router/issues/1650
Upon further inspection, it looks to be directly related to using
router.invalidate()to re-trigger the route updates. Otherwise, it looks like thethrow redirect({...})in thebeforeLoadis working as expected. Only when it's triggered viarouter.invalidate()does it seem to not passcontextcorrectlyI have a very simple reproduction for this issue as well:
https://stackblitz.com/edit/tanstack-router-b9sx1q?file=src%2Froutes%2Fabout.tsx
/aboutroute redirects withthrow redirectto/about-new/about-newwants to read the context inloader, but it receivesundefined
The only difference from the stock examples is that I have preloading turned off. If I turn on
defaultPreload: 'intent', the issue disappers.things seem to work fine for me if I use
navigateinstead ofthrow redirect. Is there a meaningful difference between the two?navigateis passed as a prop intobeforeLoad...things seem to work fine for me if I use
navigateinstead ofthrow redirect. Is there a meaningful difference between the two?navigateis passed as a prop intobeforeLoad...navigateis deprecated, see https://tanstack.com/router/latest/docs/framework/react/api/router/RouteOptionsType#beforeload-methodSeanCassiere added labelsbug
@SeanCassiere The same thing happens to me, when I navigate between routes it goes fine, but when the component enters due to a redirect, if I try to get the context in that component, it is undefined and the app dies
example error:
export const Route = createFileRoute('/auth/verify-email')({ beforeLoad: async ({ context }) => { if (!context.auth.id) { throw redirect({ to: '/auth/verify-phone', search: prev => ({ id: prev.id }), viewTransition: true }); } if (!context.auth.needEmailVerification) { if (context.auth.needPersonalInformation) { throw redirect({ to: '/onboarding/require-legal-information', search: prev => ({ id: prev.id }), viewTransition: true }); } if (!context.auth.paymentMethods || !context.auth.paymentMethods.length) { throw redirect({ to: '/payment-method/add-payment-method', search: prev => ({ id: prev.id }), viewTransition: true }); } else { throw redirect({ to: '/payment-method/select-payment-method', search: prev => ({ id: prev.id }), viewTransition: true }); } } } });hook into paymentMethodComponent
export const useAddPaymentMethodForm = () => { const { urlID, auth } = useRouteContext({ from: '/payment-method/add-payment-method' });when it enters the before load of the verify email route, the contet works, but when I do the redirect it has to add card, in the addpaymentmethod hook it is undefined
SeanCassiere closed this
Sign in to join this conversation.
Definitely experiencing something similar.
It shows up when I
redirectfrom a parent (layout) route'sbeforeLoadfunction. I moved theredirectto the child'sbeforeLoadandcontextis undefined in that same child'sloaderI'll attempt to create a repro when I have some time if it differs significantly from the one mentioned here.