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/null-history-state-depth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/router": patch
---

Don't throw on import when `history.state` is still null after `replaceState` (seen in iOS WKWebView embeds)
2 changes: 1 addition & 1 deletion src/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function saveCurrentDepth() {
if (!window.history.state || window.history.state._depth == null) {
window.history.replaceState({ ...window.history.state, _depth: window.history.length - 1 }, "");
}
depth = window.history.state._depth;
depth = window.history.state?._depth ?? window.history.length - 1;
}
if (!isServer) {
saveCurrentDepth();
Expand Down
13 changes: 12 additions & 1 deletion test/lifecycle.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { vi } from "vitest";
import { createBeforeLeave } from "../src/lifecycle.js";
import { createBeforeLeave, saveCurrentDepth } from "../src/lifecycle.js";
import type { BeforeLeaveEventArgs, Location } from "../src/types.js";

const mockLocation = { pathname: "/", search: "", hash: "", query: {}, state: null, key: "" } as unknown as Location;
Expand Down Expand Up @@ -49,3 +49,14 @@ describe("createBeforeLeave", () => {
expect(listener).not.toHaveBeenCalled();
});
});

describe("saveCurrentDepth", () => {
afterEach(() => vi.restoreAllMocks());

test("does not throw when replaceState leaves history.state null", () => {
vi.spyOn(window.history, "state", "get").mockReturnValue(null);
vi.spyOn(window.history, "replaceState").mockImplementation(() => {});

expect(() => saveCurrentDepth()).not.toThrow();
});
});
Loading