diff --git a/crates/forge-web/assets/nav.js b/crates/forge-web/assets/nav.js index f0f02ac..80c5c34 100644 --- a/crates/forge-web/assets/nav.js +++ b/crates/forge-web/assets/nav.js @@ -408,6 +408,10 @@ var link = { down: false, pending: null, post: false }; var linkBarEl = null; var alive = null; // the /-/alive stream every page holds: its open means the server is back + var streamUp = false; // the /-/alive stream is open now + var aliveSeen = false; // the stream has opened on this page: only then does its error count + var leaving = false; // the page is unloading: a stream error then is not an outage + var bannerTimer = null; // the banner waits a grace period, so a blip never shows it function linkText(name) { return (document.body && document.body.getAttribute('data-link-' + name)) || ''; @@ -436,9 +440,18 @@ } function linkDown() { - if (link.down) { return; } link.down = true; - showBar(linkText('restarting')); + armBanner(); + } + + // The bar shows only when the outage lasts: after a grace period, and never while the page is hidden + // or leaving. A hidden page that is shown again re-arms it (see wake). + function armBanner() { + if (bannerTimer) { return; } + bannerTimer = setTimeout(function () { + bannerTimer = null; + if (link.down && !leaving && !document.hidden) { showBar(linkText('restarting')); } + }, 1500); } function outage(o) { @@ -456,6 +469,7 @@ function linkBack() { if (!link.down) { return; } link.down = false; + if (bannerTimer) { clearTimeout(bannerTimer); bannerTimer = null; } var o = link.pending; link.pending = null; if (o) { hideBar(); load(o); return; } @@ -511,11 +525,29 @@ } // A `language` event means the page text changed for this user: the page is re-rendered in place // (typed text, focus and scroll kept), as for any live change, even on a page with no stream. + // The first connect may fail on a slow or switching phone network: that is no outage, so an error + // counts only after the stream has opened once. Unloading and hidden pages never show the bar. + function aliveOpen() { streamUp = true; aliveSeen = true; linkBack(); } + function aliveDown() { + streamUp = false; + if (aliveSeen && !leaving) { linkDown(); } + } + // The page is shown again (tab back, thawed, or restored from the back-forward cache): reconnect the + // stream quietly when it is not open (or always, `fresh`), and re-arm the bar if the outage is still on. + function wake(fresh) { + if (document.hidden || !alive) { return; } + if (fresh || !streamUp) { alive.restart(); } + if (link.down) { armBanner(); } + } if (window.EventSource) { - alive = keepOpen('/-/alive', linkBack, linkDown, { + alive = keepOpen('/-/alive', aliveOpen, aliveDown, { theme: applyAppearance, language: function () { refreshLive(true); } }); + document.addEventListener('visibilitychange', function () { wake(false); }); + document.addEventListener('resume', function () { wake(true); }); + window.addEventListener('pageshow', function (e) { leaving = false; if (e.persisted) { wake(true); } }); + window.addEventListener('pagehide', function () { leaving = true; }); } if ('serviceWorker' in navigator) { navigator.serviceWorker.register('/sw.js').catch(function () {}); }