diff --git a/containers/libreportal/frontend/js/components/app/services-manager.js b/containers/libreportal/frontend/js/components/app/services-manager.js index 5075f60..9601032 100644 --- a/containers/libreportal/frontend/js/components/app/services-manager.js +++ b/containers/libreportal/frontend/js/components/app/services-manager.js @@ -681,6 +681,11 @@ class ServicesManager { for (const [name] of this.openLogStreams) this._closeLogStream(name); } + // Public hook for the page lifecycle (pagehide). Closes every live log + // tail so the browser can BFCache-snapshot the page instead of being + // forced into a full reload on back/forward. + pauseStreams() { this._stopAllLogs(); } + // ------------------------------------------------------------------ // Status refresh loop (only updates dots/text, not full re-render) // ------------------------------------------------------------------ diff --git a/containers/libreportal/frontend/js/spa.js b/containers/libreportal/frontend/js/spa.js index 900c2a3..fd48dcb 100755 --- a/containers/libreportal/frontend/js/spa.js +++ b/containers/libreportal/frontend/js/spa.js @@ -170,6 +170,14 @@ class LibrePortalSPAClean { // Update browser history if (addToHistory) { history.pushState({ route: path }, '', path); + } else { + // Even for "don't add to history" navigations (initial route, back-compat + // rewrites) we stamp the current entry's state with the resolved route. + // Without this the very first history entry has state: null, so popstate + // back to it from a later pushState'd entry hits the null guard and + // does nothing — exactly the "back button doesn't work" symptom. + const here = window.location.pathname + window.location.search; + history.replaceState({ route: path }, '', here); } this.currentRoute = path; @@ -373,7 +381,7 @@ class LibrePortalSPAClean { const taskId = url.searchParams.get('task'); const canonical = window.appPath(appName, tab, sub, taskId); if (canonical !== url.pathname + url.search) { - window.history.replaceState({}, '', canonical); + window.history.replaceState({ route: canonical }, '', canonical); } } @@ -612,11 +620,36 @@ window.navigateToRoute = function(href) { } }; -// Handle browser back/forward +// Handle browser back/forward. Prefer the route stamped on the history +// entry's state; fall back to the URL bar so entries that somehow lack +// state (third-party history manipulation, very-early popstate before +// init() finished) still re-render the right page instead of no-op'ing. window.addEventListener('popstate', (e) => { - if (e.state && e.state.route && window.spaClean) { - window.spaClean.navigate(e.state.route, false); - } + if (!window.spaClean) return; + const route = (e.state && e.state.route) || (window.location.pathname + window.location.search); + window.spaClean.navigate(route, false); +}); + +// Back-forward cache (BFCache) cooperation. Open SSE/WebSocket connections +// block the browser from snapshotting the page, so the back button ends up +// doing a full reload instead of restoring instantly. Closing every live +// bus we own on pagehide lets the snapshot happen; on pageshow with +// event.persisted === true the page just came back from BFCache and we +// reopen them. Fresh loads also fire pageshow but with persisted=false — +// we ignore those because the normal init paths already started everything. +window.addEventListener('pagehide', () => { + try { window.taskEventBus && window.taskEventBus.stop(); } catch (_) {} + try { window.LiveSystem && window.LiveSystem.pause && window.LiveSystem.pause(); } catch (_) {} + try { window.servicesManager && window.servicesManager.pauseStreams && window.servicesManager.pauseStreams(); } catch (_) {} +}); + +window.addEventListener('pageshow', (e) => { + if (!e.persisted) return; + try { window.taskEventBus && window.taskEventBus.start(); } catch (_) {} + try { window.LiveSystem && window.LiveSystem.resume && window.LiveSystem.resume(); } catch (_) {} + // Service log tail streams aren't auto-resumed — the user can click + // Resume on the overlay if they were tailing a log. Most BFCache restores + // are "back to dashboard", so silently dropping the tail is fine. }); // Handle internal link clicks diff --git a/containers/libreportal/frontend/js/utils/system-live.js b/containers/libreportal/frontend/js/utils/system-live.js index a65a5de..0842471 100644 --- a/containers/libreportal/frontend/js/utils/system-live.js +++ b/containers/libreportal/frontend/js/utils/system-live.js @@ -105,6 +105,18 @@ window.LiveSystem = (() => { // Fetch the last sample synchronously (or null if the stream hasn't // produced one yet). Useful for snapshot-style reads without a sub. get last() { return last; }, + // Force-close the stream and cancel any pending reopen. Used by the + // page lifecycle (pagehide) so the browser can put the page into + // BFCache; the visibilitychange handler would normally do this too, + // but firing explicitly leaves nothing to race. + pause() { + close(); + if (reopenTimer) { clearTimeout(reopenTimer); reopenTimer = null; } + }, + // Reopen if anything is still subscribed and the document is visible. + // Called from pageshow(persisted=true) when the page is restored from + // BFCache. + resume() { open(); }, // Test hook: count of active subscribers. get subCount() { return subs.size; } };