diff --git a/crates/forge-web/assets/nav.js b/crates/forge-web/assets/nav.js index 12b1aed..8413235 100644 --- a/crates/forge-web/assets/nav.js +++ b/crates/forge-web/assets/nav.js @@ -127,7 +127,12 @@ // Stylesheets in follow the new page: missing ones are added, extra ones removed. Matched by // href, so main.css and the highlight sheet stay in place when they are the same. After a deploy // main.css has a new href: the old sheet stays until the new one has loaded, so the page is never - // unstyled, and the layout measured from it (--shell-top) is taken again once it applies. + // unstyled, and the layout measured from it (--shell-top) is taken again once it applies. A deploy + // restarts the server, so the new sheet can fail to load: it is tried again (the old one staying), + // and when it never loads the page is reloaded rather than left on a sheet that may not match it. + var SHEET_RETRY_MS = [1000, 2000, 4000, 8000, 8000]; + var sheetsLoading = {}; // href -> true while a new sheet is loading or waiting to retry + function syncStyles(doc) { var sheets = 'link[rel="stylesheet"]'; var wanted = {}; @@ -141,22 +146,32 @@ }); queueShellTop(); } - function settled() { - pending -= 1; - if (pending === 0) { dropExtras(); } + function load(source, href, tries) { + var link = document.importNode(source, true); + link.addEventListener('load', function () { + delete sheetsLoading[href]; + pending -= 1; + if (pending === 0) { dropExtras(); } + }, { once: true }); + link.addEventListener('error', function () { + link.remove(); + if (tries >= SHEET_RETRY_MS.length) { location.reload(); return; } + setTimeout(function () { load(source, href, tries + 1); }, SHEET_RETRY_MS[tries]); + }, { once: true }); + document.head.appendChild(link); } doc.head.querySelectorAll(sheets).forEach(function (l) { var href = l.getAttribute('href') || ''; - if (!have[href]) { + if (!have[href] && !sheetsLoading[href]) { have[href] = true; - var link = document.importNode(l, true); + sheetsLoading[href] = true; pending += 1; - link.addEventListener('load', settled, { once: true }); - link.addEventListener('error', settled, { once: true }); - document.head.appendChild(link); + load(l, href, 0); } }); - if (pending === 0) { dropExtras(); } + // A sheet an earlier swap is still loading drops the old ones itself once it has loaded. + var waiting = Object.keys(sheetsLoading).some(function (href) { return wanted[href]; }); + if (pending === 0 && !waiting) { dropExtras(); } } function adopt(doc, keepFields, dd, jobs) { @@ -2247,6 +2262,9 @@ window.addEventListener('scroll', queueShellTop, { passive: true }); window.addEventListener('resize', queueShellTop); + // Whatever moves the page body (a sheet that applies late, a font, the banner wrapping) changes + // the body's size, so it is measured again then too. + if (typeof ResizeObserver === 'function') { new ResizeObserver(queueShellTop).observe(document.body); } function syncRel() { var now = document.documentElement.getAttribute('data-rel-now');