fix(history): a step back never leaves the app for an older page
Chrome keeps 50 entries a tab, and when the list is full a new entry drops the oldest one the page left without a user gesture, which can be the page's own entry under a sub-view or an overlay. A programmatic back() then left the document; once it landed on an older entry whose restored Plex player started playing on its own. Every programmatic back() now asks the Navigation API first and, with no entry of this document below, settles in place: the sub-view turns to its root, an overlay's entry drops its mark, the channel page and the YouTube search clear.
This commit is contained in:
@@ -27,7 +27,21 @@ const popTo = (search: string, state: unknown = null) =>
|
||||
});
|
||||
|
||||
beforeEach(() => setUrl("/"));
|
||||
afterEach(() => vi.clearAllMocks());
|
||||
afterEach(() => vi.restoreAllMocks());
|
||||
|
||||
/** The Navigation API's view of the tab: `below` = whether each entry under the current one belongs
|
||||
* to this document, nearest first. jsdom has no Navigation API; Chrome's says this. */
|
||||
function stubNavigation(below: boolean[]) {
|
||||
const entries = [...below].reverse().map((sameDocument) => ({ sameDocument }));
|
||||
entries.push({ sameDocument: true });
|
||||
Object.defineProperty(window, "navigation", {
|
||||
configurable: true,
|
||||
value: { currentEntry: { index: entries.length - 1 }, entries: () => entries },
|
||||
});
|
||||
}
|
||||
afterEach(() => {
|
||||
Reflect.deleteProperty(window, "navigation");
|
||||
});
|
||||
|
||||
describe("useHistorySubview seed", () => {
|
||||
it("decodes the ?<key> URL param when the entry has no _sub (fresh bookmark)", () => {
|
||||
@@ -170,3 +184,40 @@ describe("useHistorySubview entry", () => {
|
||||
expect(window.history.state._sub).toEqual({ kind: "player" });
|
||||
});
|
||||
});
|
||||
|
||||
// Chrome keeps 50 entries a tab; when full, a new entry drops the oldest "skippable" one — one the page
|
||||
// left by a pushState with no user gesture — and that can be this page's own entry under the
|
||||
// sub-view. back() then left the document for an older one (measured 2026-10-07: its restored Plex
|
||||
// player started playing on its own).
|
||||
describe("useHistorySubview back", () => {
|
||||
it("goes back when the entry below is this document's", () => {
|
||||
stubNavigation([true]);
|
||||
const back = vi.spyOn(window.history, "back").mockImplementation(() => {});
|
||||
const { result } = renderHook(() => useHistorySubview("root", url));
|
||||
act(() => result.current.open("a"));
|
||||
act(() => result.current.back());
|
||||
expect(back).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("goes back as before where the browser has no Navigation API", () => {
|
||||
const back = vi.spyOn(window.history, "back").mockImplementation(() => {});
|
||||
const { result } = renderHook(() => useHistorySubview("root", url));
|
||||
act(() => result.current.open("a"));
|
||||
act(() => result.current.back());
|
||||
expect(back).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("returns to the root in place when the entry below is another document's", () => {
|
||||
const { result } = renderHook(() => useHistorySubview("root", url));
|
||||
act(() => result.current.open("a"));
|
||||
stubNavigation([false]);
|
||||
const back = vi.spyOn(window.history, "back").mockImplementation(() => {});
|
||||
const before = window.history.length;
|
||||
act(() => result.current.back());
|
||||
expect(back).not.toHaveBeenCalled();
|
||||
expect(result.current.view).toBe("root");
|
||||
expect(window.history.state?._sub).toBe("root");
|
||||
expect(window.location.search).toBe("");
|
||||
expect(window.history.length).toBe(before);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user