fix(frontend): reader back button pops history instead of pushing (0.60.2)

The reader's "back to manga" link was a naked `<a href="/manga/{id}">`
that pushed a new history entry on every tap, so browser-back kept
ping-ponging between detail and reader instead of walking out to
home:

  home → detail → reader → reader-back (push detail) → detail-back
       (pop reader) → reader-back (push detail) → … loop forever

The fix intercepts left-click and calls `window.history.back()`
directly when there's same-tab history to pop. Middle-click /
cmd-click / right-click pass through so "open in new tab" still
works, and a deep-link / fresh-tab visit where `history.length ===
1` falls through to the href so the button still leads somewhere
useful.

An earlier attempt gated the back on
`document.referrer.startsWith(origin)` — but SvelteKit's SPA
`pushState` doesn't update `document.referrer`, so that check was
always false in practice and the default href fired anyway. The
e2e regression added here uses real link clicks (not
`window.location.href`) so it actually exercises the SPA-nav path
the bug lives on.

Verified: home → detail → reader, then reader-back keeps
`history.length` at 4 (popped, not pushed), and a second tap on the
detail back button walks out to /.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
MechaCat02
2026-06-08 20:59:10 +02:00
parent 00577071fd
commit 9910a0a995
5 changed files with 197 additions and 4 deletions

View File

@@ -446,6 +446,28 @@
progressTimer = setTimeout(flushProgress, 1500);
}
/**
* Reader back behavior — pop browser history instead of pushing a
* fresh entry for `/manga/{id}`. Previously this was a naked
* `<a href>` and every tap pushed, so browser-back ping-ponged
* between detail and reader instead of walking out to home.
*
* Just check `history.length > 1` — `document.referrer` does NOT
* update across SvelteKit SPA navigations, so referrer-based
* gating silently fell back to the default href every time.
* Middle-click / cmd-click / right-click are passed through so
* "open in new tab" still works.
*/
function onBackClick(e: MouseEvent) {
if (!browser) return;
if (e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
if (window.history.length > 1) {
e.preventDefault();
window.history.back();
}
// else: let the href navigate (deep-link / fresh tab path)
}
// Single-mode: every page change moves the high-water mark.
// Intentionally NOT depending on `mode` — toggling layout doesn't
// change the read position, and re-running this effect on a mode
@@ -552,7 +574,12 @@
</svelte:head>
<nav class="reader-nav" aria-label="reader" bind:this={readerNavEl}>
<a href="/manga/{manga.id}" class="back" data-testid="back-to-manga">
<a
href="/manga/{manga.id}"
class="back"
onclick={onBackClick}
data-testid="back-to-manga"
>
<ArrowLeft size={18} aria-hidden="true" />
{#if manga.cover_image_path}
<img