diff --git a/crates/forge-web/assets/nav.js b/crates/forge-web/assets/nav.js index 475379d..d6cc4c6 100644 --- a/crates/forge-web/assets/nav.js +++ b/crates/forge-web/assets/nav.js @@ -125,23 +125,38 @@ } // 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. + // 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. function syncStyles(doc) { var sheets = 'link[rel="stylesheet"]'; var wanted = {}; doc.head.querySelectorAll(sheets).forEach(function (l) { wanted[l.getAttribute('href') || ''] = true; }); var have = {}; document.head.querySelectorAll(sheets).forEach(function (l) { have[l.getAttribute('href') || ''] = true; }); + var pending = 0; + function dropExtras() { + document.head.querySelectorAll(sheets).forEach(function (l) { + if (!wanted[l.getAttribute('href') || '']) { l.remove(); } + }); + queueShellTop(); + } + function settled() { + pending -= 1; + if (pending === 0) { dropExtras(); } + } doc.head.querySelectorAll(sheets).forEach(function (l) { var href = l.getAttribute('href') || ''; if (!have[href]) { have[href] = true; - document.head.appendChild(document.importNode(l, true)); + var link = document.importNode(l, true); + pending += 1; + link.addEventListener('load', settled, { once: true }); + link.addEventListener('error', settled, { once: true }); + document.head.appendChild(link); } }); - document.head.querySelectorAll(sheets).forEach(function (l) { - if (!wanted[l.getAttribute('href') || '']) { l.remove(); } - }); + if (pending === 0) { dropExtras(); } } function adopt(doc, keepFields, dd, jobs) { @@ -176,6 +191,7 @@ syncRel(); syncBannerExpiry(); syncShellTop(); + syncBadges(); syncDrafts(); } @@ -1960,6 +1976,21 @@ document.documentElement.style.setProperty('--shell-top', Math.round(shell.getBoundingClientRect().top) + 'px'); } + // A CI badge (`/{owner}/{repo}/badge.svg`, often in a README) is fetched again after a swap. The + // document stays the same across a soft navigation, and the browser reuses an image it already + // has at the same URL whatever its Cache-Control says, so the badge would keep an old state until + // a reload. The badge ignores query keys it does not know, so `_r` only makes the URL new. + function syncBadges() { + var stamp = String(Date.now()); + document.querySelectorAll('img[src*="/badge.svg"]').forEach(function (img) { + var url; + try { url = new URL(img.getAttribute('src'), location.href); } catch (err) { return; } + if (url.origin !== location.origin || !/\/badge\.svg$/.test(url.pathname)) { return; } + url.searchParams.set('_r', stamp); + img.src = url.pathname + url.search; + }); + } + function queueShellTop() { if (!shellFrame) { shellFrame = requestAnimationFrame(syncShellTop); } }