Nav Back: track popstate delta so back-then-forward keeps accurate depth

Codex's remaining caveat: appNavDepth decremented on every popstate, so a
browser Back then Forward undercounted (in-page Back would jump to Highlights
early). Use the navigation's signed delta on popstate (Back -1, Forward +1,
±N for jumps) instead of a flat decrement, so the depth stays accurate through
any back/forward dance. Falls back to -1 if delta is unavailable (safe).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
jay
2026-06-08 09:15:43 -04:00
parent eb91a2f856
commit 489c34d2f2
+4 -3
View File
@@ -276,10 +276,11 @@
// The initial 'enter' is handled by onMount, after its data deps are fetched.
afterNavigate((nav) => {
if (nav.type === 'enter') return;
// Forward in-app nav deepens the stack; back/forward (popstate) unwinds it.
// Clamped at 0, so an out-of-app landing keeps Back app-safe (→ Highlights).
// Forward in-app nav deepens the stack; popstate moves by its signed delta
// (Back = -1, Forward = +1, ±N for jumps), so a back-then-forward dance keeps
// an accurate count. Clamped at 0, so an out-of-app landing stays app-safe.
if (nav.type === 'goto' || nav.type === 'link') appNavDepth += 1;
else if (nav.type === 'popstate') appNavDepth = Math.max(0, appNavDepth - 1);
else if (nav.type === 'popstate') appNavDepth = Math.max(0, appNavDepth + (nav.delta ?? -1));
loadView(selected);
});