fix: skip blockers on non-cancelable navigate events#227
Merged
Conversation
Per the Navigation API spec, some navigate events are not cancelable (notably certain back/forward traversals, depending on browser and user activation). Calling preventDefault() on them is ignored, so blockers would run shouldBlock() — possibly showing a confirm() dialog — while the navigation proceeded regardless of the user's answer. Check event.cancelable before running blockers so shouldBlock() is skipped entirely when the navigation cannot be prevented, and document the caveat in the useBlocker docs. Fixes #211 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JBczYkJZ5sdLWD4ue9zy37
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #211
Problem
When a blocker registered via
useBlockerwas active, the navigate handler calledevent.preventDefault()unconditionally. Per the Navigation API spec, some navigate events are not cancelable — notably certain back/forward traversals, depending on the browser and user activation. On those eventspreventDefault()is ignored, soshouldBlock()would run (possibly showing aconfirm()dialog) while the navigation proceeded anyway — the user confirms "stay" and leaves regardless.Changes
NavigationAPIAdapter.setupInterception: guard the blocker check withevent.cancelable. When the event cannot be canceled,shouldBlock()is skipped entirely (no dialog whose answer can't be honored) and the navigation is handled normally (interception, loaders, etc.).__tests__/setup.ts): mock navigate events now carry acancelableflag (defaulttrue),preventDefault()mimics real DOM behavior by being a no-op on non-cancelable events, and__simulateNavigationWithEventaccepts acancelableoption.shouldBlockis never called, nothing is prevented, and the route is still intercepted.useBlockerJSDoc,docs/useBlocker-design.md, and the docs site (ApiHooksPage.tsx) now document that back/forward traversals may not be blockable in some browsers/situations, alongside the existingbeforeunloadcaveat.Verification
pnpm --filter @funstack/router test:run— 329 tests pass, no type errorspnpm typecheck,pnpm lint,pnpm format:check— all clean🤖 Generated with Claude Code
https://claude.ai/code/session_01JBczYkJZ5sdLWD4ue9zy37
Generated by Claude Code