You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
onBeforeNavigate not invoked after redirect within beforeLoad method was invoked #3920
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.
This bug also breaks the Sentry TanStack Router integration because it hooks into the onBeforeNavigate to generate navigation spans for the tracing feature.
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);}});
Activity
schiller-manuel commentedon Apr 2, 2025
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
JulianAnthes commentedon Apr 4, 2025
Thank you for confirming this observation by finding the possible root cause. 🙏
For now we'll set
reloadDocument: truewhen usingredirect()withinbeforeLoadin 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. 🙈
wtlin1228 commentedon Dec 10, 2025
Same problem 😢
wtlin1228 commentedon Dec 11, 2025
Switching to this works for me:
Since the
onBeforeLoadwill be emitted right afteronBeforeNavigate, andonBeforeLoadwill not check thethis.state.redirect.ref: https://github.com/TanStack/router/blob/main/packages/router-core/src/router.ts#L2113-L2129
fix auth state not working on first load, fix onBeforeNavigate not fi…
ambroselittle commentedon Jun 2, 2026
Just wasted an hour or so discovering this was the cause of unexpectedly not getting events after an SSO handler throws redirect. :sadpanda:
longzheng commentedon Jul 5, 2026
This bug also breaks the Sentry TanStack Router integration because it hooks into the
onBeforeNavigateto generate navigation spans for the tracing feature.longzheng commentedon Jul 5, 2026
I'm using a workaround that at least fixes subsequent
onBeforeLoadevents to trigger upon the first successful navigation/route load