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:
@@ -12,6 +12,7 @@
|
||||
import { useCallback, useEffect, useEffectEvent, useMemo, useState } from "react";
|
||||
import { navPosition, restoreNavPosition, type NavPosition } from "./listNav";
|
||||
import { urlWithParams } from "./urlState";
|
||||
import { useLatest } from "./useLatest";
|
||||
|
||||
// --- Per-history-entry scroll id + a nav bus (for scroll restoration) ------------------------
|
||||
// Every navigation entry carries a stable numeric id in history.state.sfScroll. PageScroller
|
||||
@@ -96,7 +97,29 @@ export function rememberNavPosition(): Record<string, unknown> {
|
||||
return st;
|
||||
}
|
||||
|
||||
// --- Is the entry a back() lands on still ours? --------------------------------------------
|
||||
// Chrome keeps 50 entries a tab. When the list is full a new entry drops the OLDEST "skippable" one —
|
||||
// an entry the page left by a pushState without a user gesture (an auto-skip countdown, a card that
|
||||
// opened on its own) — not the oldest entry, so it can be this page's own entry under a sub-view.
|
||||
// A programmatic back() then leaves the document for whatever older page sits below (measured
|
||||
// 2026-10-07: an older entry whose restored Plex player started playing on its own). The Navigation
|
||||
// API says which document the entry a back() lands on belongs to — counting the back()s of ours still
|
||||
// in flight; a browser without it keeps the old assumption that our entries are all still there.
|
||||
interface NavigationLike {
|
||||
currentEntry: { index: number } | null;
|
||||
entries: () => readonly { sameDocument: boolean }[];
|
||||
}
|
||||
|
||||
/** Would one more history.back() still land in this document? */
|
||||
export function backStaysInDocument(): boolean {
|
||||
const nav = (window as { navigation?: NavigationLike }).navigation;
|
||||
const at = nav?.currentEntry?.index;
|
||||
if (nav == null || at == null) return true;
|
||||
return nav.entries()[at - pendingProgrammatic - 1]?.sameDocument === true;
|
||||
}
|
||||
|
||||
// --- Sub-views (value carried in history.state._sub) ---------------------------------------
|
||||
|
||||
/** Optional address-bar mirroring for a sub-view, so a deep sub-view (e.g. a Plex show page) is
|
||||
* bookmarkable. history.state._sub stays the source of truth for Back/Forward; this only adds the URL. */
|
||||
export interface SubviewUrl<T> {
|
||||
@@ -175,7 +198,13 @@ export function useHistorySubview<T>(
|
||||
},
|
||||
[url],
|
||||
);
|
||||
const back = useCallback(() => window.history.back(), []);
|
||||
// With no entry of this document below (see backStaysInDocument), the sub-view's own entry becomes
|
||||
// the root view in place — a step up the module, not out of the app.
|
||||
const rootRef = useLatest(root);
|
||||
const back = useCallback(() => {
|
||||
if (backStaysInDocument()) window.history.back();
|
||||
else open(rootRef.current, { replace: true });
|
||||
}, [open, rootRef]);
|
||||
// Memoised as a whole for the same reason: a consumer that wants "re-run when the sub-view moves"
|
||||
// can now write `[sub]` and mean it, instead of re-running on every parent render.
|
||||
return useMemo(() => ({ view, open, back }), [view, open, back]);
|
||||
@@ -228,8 +257,14 @@ function reconcileHistory(): void {
|
||||
}
|
||||
while (historyDepth > want) {
|
||||
historyDepth--;
|
||||
pendingProgrammatic++;
|
||||
window.history.back();
|
||||
if (backStaysInDocument()) {
|
||||
pendingProgrammatic++;
|
||||
window.history.back();
|
||||
} else {
|
||||
// Nothing of ours below to step back to (see backStaysInDocument): the entry stays, unmarked.
|
||||
const { _ov: _drop, ...st } = (window.history.state ?? {}) as Record<string, unknown>;
|
||||
window.history.replaceState(st, "");
|
||||
}
|
||||
}
|
||||
syncWaiters.forEach((cb) => cb());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user