From 93ac1142f488d65d10eb98cc3781877af40b436c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 23 Oct 2026 16:30:16 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20reconnect=20banner=20wait?= =?UTF-8?q?s=20for=20a=20real=20outage=20instead=20of=20flashing=20on=20pa?= =?UTF-8?q?ge=20loads?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The banner came from every error of the /-/alive stream, including the first failed connect, unloads, hidden or frozen tabs and bfcache restores, and without any grace period. It now shows only after the stream has opened on the page and stays down for 1.5 s, never while the page is hidden or leaving. A page shown again reconnects quietly. --- crates/forge-web/assets/nav.js | 38 +++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) 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 () {}); }