forge
Explore
Log in

Don't you dare go Hollow

Reconnect banner waits for a real outage instead of flashing on page loads

Browse files at this commit

93ac1142f488d65d10eb98cc3781877af40b436cAhmet Kaan GümüşParent: 3535e27
  • fix
    • reconnect banner waits for a real outage instead of flashing on page loads
Show full message
🐛 fix: reconnect banner waits for a real outage instead of flashing on page loads

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.jsmodified+35 -3
@@ -408,6 +408,10 @@
408 var link = { down: false, pending: null, post: false };408 var link = { down: false, pending: null, post: false };
409 var linkBarEl = null;409 var linkBarEl = null;
410 var alive = null; // the /-/alive stream every page holds: its open means the server is back410 var alive = null; // the /-/alive stream every page holds: its open means the server is back
411+ var streamUp = false; // the /-/alive stream is open now
412+ var aliveSeen = false; // the stream has opened on this page: only then does its error count
413+ var leaving = false; // the page is unloading: a stream error then is not an outage
414+ var bannerTimer = null; // the banner waits a grace period, so a blip never shows it
411 415
412 function linkText(name) {416 function linkText(name) {
413 return (document.body && document.body.getAttribute('data-link-' + name)) || '';417 return (document.body && document.body.getAttribute('data-link-' + name)) || '';
@@ -436,9 +440,18 @@
436 }440 }
437 441
438 function linkDown() {442 function linkDown() {
439- if (link.down) { return; }
440 link.down = true;443 link.down = true;
441- showBar(linkText('restarting'));444+ armBanner();
445+ }
446+
447+ // The bar shows only when the outage lasts: after a grace period, and never while the page is hidden
448+ // or leaving. A hidden page that is shown again re-arms it (see wake).
449+ function armBanner() {
450+ if (bannerTimer) { return; }
451+ bannerTimer = setTimeout(function () {
452+ bannerTimer = null;
453+ if (link.down && !leaving && !document.hidden) { showBar(linkText('restarting')); }
454+ }, 1500);
442 }455 }
443 456
444 function outage(o) {457 function outage(o) {
@@ -456,6 +469,7 @@
456 function linkBack() {469 function linkBack() {
457 if (!link.down) { return; }470 if (!link.down) { return; }
458 link.down = false;471 link.down = false;
472+ if (bannerTimer) { clearTimeout(bannerTimer); bannerTimer = null; }
459 var o = link.pending;473 var o = link.pending;
460 link.pending = null;474 link.pending = null;
461 if (o) { hideBar(); load(o); return; }475 if (o) { hideBar(); load(o); return; }
@@ -511,11 +525,29 @@
511 }525 }
512 // A `language` event means the page text changed for this user: the page is re-rendered in place526 // A `language` event means the page text changed for this user: the page is re-rendered in place
513 // (typed text, focus and scroll kept), as for any live change, even on a page with no stream.527 // (typed text, focus and scroll kept), as for any live change, even on a page with no stream.
528+ // The first connect may fail on a slow or switching phone network: that is no outage, so an error
529+ // counts only after the stream has opened once. Unloading and hidden pages never show the bar.
530+ function aliveOpen() { streamUp = true; aliveSeen = true; linkBack(); }
531+ function aliveDown() {
532+ streamUp = false;
533+ if (aliveSeen && !leaving) { linkDown(); }
534+ }
535+ // The page is shown again (tab back, thawed, or restored from the back-forward cache): reconnect the
536+ // stream quietly when it is not open (or always, `fresh`), and re-arm the bar if the outage is still on.
537+ function wake(fresh) {
538+ if (document.hidden || !alive) { return; }
539+ if (fresh || !streamUp) { alive.restart(); }
540+ if (link.down) { armBanner(); }
541+ }
514 if (window.EventSource) {542 if (window.EventSource) {
515- alive = keepOpen('/-/alive', linkBack, linkDown, {543+ alive = keepOpen('/-/alive', aliveOpen, aliveDown, {
516 theme: applyAppearance,544 theme: applyAppearance,
517 language: function () { refreshLive(true); }545 language: function () { refreshLive(true); }
518 });546 });
547+ document.addEventListener('visibilitychange', function () { wake(false); });
548+ document.addEventListener('resume', function () { wake(true); });
549+ window.addEventListener('pageshow', function (e) { leaving = false; if (e.persisted) { wake(true); } });
550+ window.addEventListener('pagehide', function () { leaving = true; });
519 }551 }
520 if ('serviceWorker' in navigator) { navigator.serviceWorker.register('/sw.js').catch(function () {}); }552 if ('serviceWorker' in navigator) { navigator.serviceWorker.register('/sw.js').catch(function () {}); }
521 553