Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/history-depth-capped-length.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/router": patch
---

Stamp a pushed history entry's `_depth` as one above the current entry instead of `history.length - 1`. Browsers cap session history (50 entries in Chromium and Firefox), so once the cap was reached every new entry got the same depth. That broke scroll restoration and `useBeforeLeave` back-navigation deltas on long sessions.
15 changes: 13 additions & 2 deletions src/routers/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ function keepDepth(state: any) {
};
}

// A pushed entry sits one above the current one. Stamp it from `depth`, not
// `history.length`: once the browser's session history is full (50 entries in
// Chromium and Firefox) a push drops the oldest entry and `length` stays the
// same, so `length - 1` would give every new entry the depth of the one below.
function nextDepth(state: any) {
return {
...state,
_depth: (depth ?? window.history.length - 1) + 1
};
}

function notifyIfNotBlocked(
notify: (value?: string | LocationChange) => void,
block: (delta: number | null) => boolean
Expand Down Expand Up @@ -87,7 +98,7 @@ export function browserHistory(): RouterHistory {
if (replace) {
window.history.replaceState(keepDepth(state), "", value);
} else {
window.history.pushState(state, "", value);
window.history.pushState(nextDepth(state), "", value);
}
scrollToHash(decodeURIComponent(window.location.hash.slice(1)), scroll);
saveCurrentDepth();
Expand Down Expand Up @@ -136,7 +147,7 @@ export function hashHistory(): RouterHistory {
if (replace) {
window.history.replaceState(keepDepth(state), "", "#" + value);
} else {
window.history.pushState(state, "", "#" + value);
window.history.pushState(nextDepth(state), "", "#" + value);
}
const hashIndex = value.indexOf("#");
const hash = hashIndex >= 0 ? value.slice(hashIndex + 1) : "";
Expand Down
40 changes: 39 additions & 1 deletion test/browser-history.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, test, vi } from "vitest";
import { browserHistory } from "../src/index.js";
import { browserHistory, hashHistory } from "../src/index.js";

describe("browserHistory", () => {
afterEach(() => vi.restoreAllMocks());
Expand All @@ -10,4 +10,42 @@ describe("browserHistory", () => {

expect(() => browserHistory()).not.toThrow();
});

// #694: browsers cap session history (50 entries in Chromium and Firefox).
// Once full, a push drops the oldest entry and history.length stops growing.
test("stamps increasing depths on push when history.length is capped", () => {
window.history.replaceState({ _depth: 48 }, "", "/");
vi.spyOn(window.history, "length", "get").mockReturnValue(50);
const history = browserHistory();

const depths: number[] = [];
for (let i = 1; i <= 3; i++) {
history.set({ value: `/${i}` });
depths.push(window.history.state._depth);
}
expect(depths).toEqual([49, 50, 51]);

history.set({ value: "/3?replaced", replace: true });
expect(window.history.state._depth).toBe(51);
});

test("keeps user state when stamping the depth on push", () => {
window.history.replaceState({ _depth: 3 }, "", "/");
const history = browserHistory();
history.set({ value: "/next", state: { from: "test" } });
expect(window.history.state).toEqual({ from: "test", _depth: 4 });
});

test("hashHistory stamps increasing depths on push when history.length is capped", () => {
window.history.replaceState({ _depth: 48 }, "", "/");
vi.spyOn(window.history, "length", "get").mockReturnValue(50);
const history = hashHistory();

const depths: number[] = [];
for (let i = 1; i <= 3; i++) {
history.set({ value: `/${i}` });
depths.push(window.history.state._depth);
}
expect(depths).toEqual([49, 50, 51]);
});
});