Skip to content
Source
Sign inSign up
TanStack/router

Undefined context after redirect #1670

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

Describe 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 beforeLoad function 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

  1. Open repro project in stackblitz
  2. Click the Click me to crash link
  3. 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

  1. Definitely experiencing something similar.

    It shows up when I redirect from a parent (layout) route's beforeLoad function. I moved the redirect to the child's beforeLoad and context is undefined in that same child's loader

    I'll attempt to create a repro when I have some time if it differs significantly from the one mentioned here.

  2. Upon further inspection, it looks to be directly related to using router.invalidate() to re-trigger the route updates. Otherwise, it looks like the throw redirect({...}) in the beforeLoad is working as expected. Only when it's triggered via router.invalidate() does it seem to not pass context correctly

  3. I have a very simple reproduction for this issue as well:

    https://stackblitz.com/edit/tanstack-router-b9sx1q?file=src%2Froutes%2Fabout.tsx

    • /about route redirects with throw redirect to /about-new
    • /about-new wants to read the context in loader, but it receives undefined

    The only difference from the stock examples is that I have preloading turned off. If I turn on defaultPreload: 'intent', the issue disappers.

  4. things seem to work fine for me if I use navigate instead of throw redirect. Is there a meaningful difference between the two? navigate is passed as a prop into beforeLoad ...

  5. things seem to work fine for me if I use navigate instead of throw redirect. Is there a meaningful difference between the two? navigate is passed as a prop into beforeLoad ...

    navigate is deprecated, see https://tanstack.com/router/latest/docs/framework/react/api/router/RouteOptionsType#beforeload-method Screenshot 2024-06-05 at 19 42 40

  6. SeanCassiere added labelsbug

  7. @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

  8. SeanCassiere closed this

Sign in to join this conversation.