Skip to content

fix(history): stamp pushed depth from the current entry, not history.length - #695

Open
sarankumar1325 wants to merge 1 commit into
solidjs:nextfrom
sarankumar1325:fix/history-depth-capped-length
Open

sarankumar1325 wants to merge 1 commit into
solidjs:nextfrom
sarankumar1325:fix/history-depth-capped-length

Conversation

@sarankumar1325

Copy link
Copy Markdown

Fixes #694

Problem

saveCurrentDepth() stamps an unstamped entry with _depth: history.length - 1, and browserHistory().set / hashHistory().set pushed the caller's state without a depth, so every router push ended up stamped that way. Browsers cap session history (50 entries in Chromium and Firefox). Once the cap is reached, pushState drops the oldest entry and history.length stays the same, so each new entry got the same _depth as the one below it. Back then computed delta = 0 (which skipped useBeforeLeave), and scroll restoration put consecutive entries in the same slot.

Change

  • src/routers/history.ts: on push, both browserHistory() and hashHistory() now stamp the new entry with depth + 1, where depth is the current entry's depth. If no depth is known yet, it falls back to history.length - 1. Replace still keeps the current depth (keepDepth). User state is kept as before, and getSource still hides state that holds only _depth.
  • test/browser-history.spec.ts: three tests. Two mock history.length at the 50-entry cap, push three times, and expect depths 49, 50 and 51 (browser and hash). The third checks that user state is kept next to the stamp. Without the change, the capped tests get [49, 49, 49].
  • A patch changeset.

Not covered: entries the router doesn't create (for example a plain #anchor jump handled by the browser) are still stamped by saveCurrentDepth() from history.length, the same as today.

main (1.0.0) has the same pattern in set() of Router / HashRouter in src/lifecycle.ts. I can open a matching PR there if you want it backported.

Testing

  • pnpm test (client and server configs): 720 + 96 passed
  • pnpm run test:types
  • prettier --check on the changed files

AI disclosure: I used an AI assistant to help write this change and its tests. I reviewed the diff and ran the checks above.

…length

Browsers cap session history (50 entries in Chromium and Firefox). Once
the cap is reached, pushState drops the oldest entry and history.length
stays the same, so `history.length - 1` gave every new entry the depth
of the one below it. Back then computed a zero delta (useBeforeLeave was
skipped) and scroll restoration shared one slot between entries.

browserHistory() and hashHistory() now stamp a pushed entry with the
current depth + 1. Replace keeps the current depth as before.

Fixes solidjs#694
@changeset-bot

changeset-bot Bot commented Oct 23, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1865ccc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@solidjs/router Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant