Skip to content

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

@JulianAnthes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions