Skip to content

onBeforeNavigate not invoked after redirect within beforeLoad method was invoked #3920

Closed

Description

@JulianAnthes

Which project does this relate to?

Router

Describe the bug

When using throw redirect() within a beforeLoad function, subsequent router.subscribe("onBeforeNavigate") events may fail to trigger, causing potential data loss for components that rely on this event for cleanup or persistence operations.

Your Example Website or App

https://github.com/dschu-lab/tanstack-router-onbeforeload-redirect-issue

Steps to Reproduce the Bug or Issue

  • Start with an unauthenticated state
  • Authenticate user (which triggers a redirect() in beforeLoad)
  • Navigate to a page with onBeforeNavigate subscriptions
  • Attempt to navigate away from this page
  • Expected onBeforeNavigate event is not triggered

Subscription to the event is implemented at:
https://github.com/dschu-lab/tanstack-router-onbeforeload-redirect-issue/blob/2eb4bd98e7be565983eb7bac8c889859e51da8f3/src/components/PersistForm/PersistForm.tsx#L44-L51

Expected behavior

The onBeforeNavigate event should consistently trigger regardless of whether a redirect() was previously thrown in a beforeLoad or loader function.

Screenshots or Videos

Bildschirmaufnahme.2025-04-02.um.16.46.05.mp4

Platform

  • OS: macOS 15.3.2
  • Browser: Chrome
  • Version: 134.0

Additional context

No response

Activity

  1. schiller-manuel commented on Apr 2, 2025

    @schiller-manuel
    Collaborator
  2. JulianAnthes commented on Apr 4, 2025

    @JulianAnthes
    Author

    looks like the redirect state is never reset and thus the event is not emitted anymore https://github.com/TanStack/router/blob/f8015e7629307499d4d6245077ad84145b6064a7/packages/router-core/src/router.ts#L2017C16-L2017C35

    Thank you for confirming this observation by finding the possible root cause. 🙏

    For now we'll set reloadDocument: true when using redirect() within beforeLoad in our SPA as a workaround to bypass this issue.

    I would like to submit a fix for this myself, but could not figure out when the redirect state is set and when exactly it should be cleared again. 🙈

  3. wtlin1228 commented on Dec 10, 2025

    @wtlin1228
    Contributor

    Same problem 😢

  4. wtlin1228 commented on Dec 11, 2025

    @wtlin1228
    Contributor

    Switching to this works for me:

    router.subscribe("onBeforeLoad", (...args) => {
      // ...
    });

    Since the onBeforeLoad will be emitted right after onBeforeNavigate, and onBeforeLoad will not check the this.state.redirect .

    ref: https://github.com/TanStack/router/blob/main/packages/router-core/src/router.ts#L2113-L2129

  5. ambroselittle commented on Jun 2, 2026

    @ambroselittle

    Just wasted an hour or so discovering this was the cause of unexpectedly not getting events after an SSO handler throws redirect. :sadpanda:

  6. longzheng commented on Jul 5, 2026

    @longzheng

    This bug also breaks the Sentry TanStack Router integration because it hooks into the onBeforeNavigate to generate navigation spans for the tracing feature.

  7. longzheng commented on Jul 5, 2026

    @longzheng

    I'm using a workaround that at least fixes subsequent onBeforeLoad events to trigger upon the first successful navigation/route load

    // TanStack Router leaves stores.redirect set after an initial-load redirect,
    // which permanently suppresses onBeforeNavigate (and Sentry navigation spans).
    // Workaround by clearing the redirect store after a successful navigation, so that onBeforeNavigate is called for subsequent navigations.
    router.subscribe('onResolved', () => {
        if (router.stores.redirect.state) {
            router.stores.redirect.setState(() => undefined);
        }
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions