From 1889dfe5b0f5abcda759ad8790500afd4d4f9489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 29 Oct 2026 04:53:17 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20a=20deploy=20no=20longer?= =?UTF-8?q?=20leaves=20the=20sidebar=20a=20sliver=20at=20the=20top:=20when?= =?UTF-8?q?=20the=20new=20stylesheet=20failed=20to=20load=20while=20the=20?= =?UTF-8?q?server=20restarted,=20the=20old=20one=20was=20dropped=20anyway,?= =?UTF-8?q?=20so=20the=20page=20went=20unstyled=20and=20the=20sidebar's=20?= =?UTF-8?q?height=20was=20measured=20from=20that;=20now=20the=20old=20shee?= =?UTF-8?q?t=20stays=20until=20the=20new=20one=20loads,=20the=20new=20one?= =?UTF-8?q?=20is=20tried=20again=20(the=20page=20reloads=20if=20it=20never?= =?UTF-8?q?=20loads),=20and=20the=20layout=20is=20measured=20again=20whene?= =?UTF-8?q?ver=20the=20page's=20size=20changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/forge-web/assets/nav.js | 38 +++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 10 deletions(-) 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');