Repository navigation
Description
Which project does this relate to?
Router
Describe the bug
A same-document history entry that the browser creates on its own, such as a fragment navigation from a plain <a href="#fragment">, has no router state. parseHref fills in __TSR_index: 0 for it, so in createBrowserHistory's popstate handler every delta measured across that entry is wrong:
const delta =
nextLocation.state[stateIndexKey] - currentLocation.state[stateIndexKey]When a blocker refuses such a traversal, the undo win.history.go(-delta) uses that wrong delta. From /?step=0 → /?step=1 → /?step=1#fragment:
- Back reaches
/?step=1(index 1) from the fragment entry (read as 0): delta +1, reported asFORWARD, "undone" withgo(-1), one entry further back, to/?step=0. go(-2)reaches/?step=0(0) from the fragment entry (0): delta 0, undone withgo(0), so the refused traversal stands (and per the HTML specgo(0)is a reload; withenableBeforeUnloadon, that surfaces as the native "Leave site?" dialog).
Either way, after the user chooses "Stay here" they are on an entry they never chose, which is what the blocker exists to prevent. Router-created entries are unaffected; createBrowserHistory already stamps the initial entry when it has no state, but not entries the browser creates later.
Complete minimal reproducer
Steps to Reproduce the Bug
npm install && npm run dev- Click Add history entry (Step 1), then Plain fragment link (
/?step=1#fragment). - Type in Draft (activates
useBlockerwithwithResolver). - Click History back (or History go(-2)), then Stay here.
- The app is on
/?step=0.
tests/repro.spec.ts automates both cases; the repository's Actions run shows both failing on 1.170.39: https://github.com/a777med/tanstack-router-fragment-blocker-repro/actions/runs/36088338040
Expected behavior
As a user, I expected "Stay here" to keep me on /?step=1#fragment with my draft, but I am moved to /?step=0.
Platform
- Router / Start Version:
@tanstack/react-router1.170.39 (@tanstack/history1.162.4; same code onmain) - OS: Linux (GitHub Actions
ubuntu-latest), macOS - Browser: Chromium (Playwright 1.63.0)
- Browser Version: bundled with Playwright 1.63.0
- Bundler: vite
- Bundler Version: 8.3.1
Additional context
A fix with unit and e2e coverage follows as a PR: on popstate, stamp an entry without router state with the index one past the entry it was created from (the same stamp the initial entry gets), using the unpatched replaceState, before measuring the delta.
Activity