fix(history): restore blocked pop navigation by delta - #7908
Open
erictheswift wants to merge 3 commits into
Open
fix(history): restore blocked pop navigation by delta#7908erictheswift wants to merge 3 commits into
erictheswift wants to merge 3 commits into
Conversation
Contributor
📝 WalkthroughWalkthroughBlocked browser-history pop navigations now use the inverse computed delta for rollback, while zero or unsafe deltas are accepted without rollback. Tests cover forward, multi-entry, zero-delta, and legacy-entry blocked navigation. ChangesBrowser history rollback
Estimated code review effort: 3 (Moderate) | ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Problem
When a browser POP is blocked,
createBrowserHistoryalways callswindow.history.go(1)to undo it. That only reverses a single-entry Back:go(-2)returns only one entry, leaving the browser URL and therouter's current location on different history entries.
There are also cases where the POP has already happened, but its computed delta
is unusable:
delta === 0;use TanStack metadata while an older
history@4{ key, state }entry has no__TSR_index, producingdelta === NaN.Passing either value back to
window.history.go(-delta)can reload the currentpage instead of restoring the entry that was active before the POP.
Fix
Undo an observed POP with its inverse delta:
window.history.go(-delta).For Back this remains
go(1), while Forward and multi-entry GO operations nowreturn to the exact entry from which navigation started.
When the delta is zero or otherwise not a safe integer, the original entry
cannot be recovered without guessing a direction. In that case, accept the
already-completed browser traversal, synchronize
currentLocation, and notifysubscribers instead of passing an unusable delta to
history.go.This means a blocker cannot restore a POP whose displacement is unknowable; the
browser traversal stands. That is intentional and avoids both a destructive
reload and an arbitrary move to the wrong history entry.
Tests
Added browser-history regressions for:
go(-2);delta === 0, asserting that nohistory.gocall occursand subscribers observe the accepted location with action index
0;previous entry still has the
history@4{ key, state }shape. The testexplicitly observes
delta === NaN, verifies thathistory.gois not called,and verifies that browser history,
currentLocation, state, and subscribersall adopt the already-opened legacy entry.
Validated with:
The Forward and multi-entry tests fail on
mainby restoring the wrong entry.The zero-delta regression fails on the previous implementation by attempting
history.go(0). The hybrid-history regression independently pins thenon-safe-integer branch: removing only that guard leaves the zero-delta test
green while the legacy traversal fails.
Summary by CodeRabbit
Bug Fixes
Tests