A deploy no longer leaves the sidebar a sliver at the top: when the new stylesheet failed to load while the server restarted, the old one was dropped anyway, so the page went unstyled and the sidebar's height was measured from that
Browse files at this commit1889dfe5b0f5abcda759ad8790500afd4d4f9489Parent: dcdcd27- fix
- a deploy no longer leaves the sidebar a sliver at the top: when the new stylesheet failed to load while the server restarted, the old one was dropped anyway, so the page went unstyled and the sidebar's height was measured from that
- now the old sheet stays until the new one loads, the new one is tried again (the page reloads if it never loads), and the layout is measured again whenever the page's size changes
1 changed file +28 -10
- crates/forge-web/assets/nav.js +28 -10
crates/forge-web/assets/nav.jsmodified+28 -10
| @@ -127,7 +127,12 @@ | |||
| 127 | // Stylesheets in <head> follow the new page: missing ones are added, extra ones removed. Matched by | 127 | // Stylesheets in <head> follow the new page: missing ones are added, extra ones removed. Matched by |
| 128 | // href, so main.css and the highlight sheet stay in place when they are the same. After a deploy | 128 | // href, so main.css and the highlight sheet stay in place when they are the same. After a deploy |
| 129 | // main.css has a new href: the old sheet stays until the new one has loaded, so the page is never | 129 | // main.css has a new href: the old sheet stays until the new one has loaded, so the page is never |
| 130 | - // unstyled, and the layout measured from it (--shell-top) is taken again once it applies. | 130 | + // unstyled, and the layout measured from it (--shell-top) is taken again once it applies. A deploy |
| 131 | + // restarts the server, so the new sheet can fail to load: it is tried again (the old one staying), | ||
| 132 | + // and when it never loads the page is reloaded rather than left on a sheet that may not match it. | ||
| 133 | + var SHEET_RETRY_MS = [1000, 2000, 4000, 8000, 8000]; | ||
| 134 | + var sheetsLoading = {}; // href -> true while a new sheet is loading or waiting to retry | ||
| 135 | + | ||
| 131 | function syncStyles(doc) { | 136 | function syncStyles(doc) { |
| 132 | var sheets = 'link[rel="stylesheet"]'; | 137 | var sheets = 'link[rel="stylesheet"]'; |
| 133 | var wanted = {}; | 138 | var wanted = {}; |
| @@ -141,22 +146,32 @@ | |||
| 141 | }); | 146 | }); |
| 142 | queueShellTop(); | 147 | queueShellTop(); |
| 143 | } | 148 | } |
| 144 | - function settled() { | 149 | + function load(source, href, tries) { |
| 145 | - pending -= 1; | 150 | + var link = document.importNode(source, true); |
| 146 | - if (pending === 0) { dropExtras(); } | 151 | + link.addEventListener('load', function () { |
| 152 | + delete sheetsLoading[href]; | ||
| 153 | + pending -= 1; | ||
| 154 | + if (pending === 0) { dropExtras(); } | ||
| 155 | + }, { once: true }); | ||
| 156 | + link.addEventListener('error', function () { | ||
| 157 | + link.remove(); | ||
| 158 | + if (tries >= SHEET_RETRY_MS.length) { location.reload(); return; } | ||
| 159 | + setTimeout(function () { load(source, href, tries + 1); }, SHEET_RETRY_MS[tries]); | ||
| 160 | + }, { once: true }); | ||
| 161 | + document.head.appendChild(link); | ||
| 147 | } | 162 | } |
| 148 | doc.head.querySelectorAll(sheets).forEach(function (l) { | 163 | doc.head.querySelectorAll(sheets).forEach(function (l) { |
| 149 | var href = l.getAttribute('href') || ''; | 164 | var href = l.getAttribute('href') || ''; |
| 150 | - if (!have[href]) { | 165 | + if (!have[href] && !sheetsLoading[href]) { |
| 151 | have[href] = true; | 166 | have[href] = true; |
| 152 | - var link = document.importNode(l, true); | 167 | + sheetsLoading[href] = true; |
| 153 | pending += 1; | 168 | pending += 1; |
| 154 | - link.addEventListener('load', settled, { once: true }); | 169 | + load(l, href, 0); |
| 155 | - link.addEventListener('error', settled, { once: true }); | ||
| 156 | - document.head.appendChild(link); | ||
| 157 | } | 170 | } |
| 158 | }); | 171 | }); |
| 159 | - if (pending === 0) { dropExtras(); } | 172 | + // A sheet an earlier swap is still loading drops the old ones itself once it has loaded. |
| 173 | + var waiting = Object.keys(sheetsLoading).some(function (href) { return wanted[href]; }); | ||
| 174 | + if (pending === 0 && !waiting) { dropExtras(); } | ||
| 160 | } | 175 | } |
| 161 | 176 | ||
| 162 | function adopt(doc, keepFields, dd, jobs) { | 177 | function adopt(doc, keepFields, dd, jobs) { |
| @@ -2247,6 +2262,9 @@ | |||
| 2247 | 2262 | ||
| 2248 | window.addEventListener('scroll', queueShellTop, { passive: true }); | 2263 | window.addEventListener('scroll', queueShellTop, { passive: true }); |
| 2249 | window.addEventListener('resize', queueShellTop); | 2264 | window.addEventListener('resize', queueShellTop); |
| 2265 | + // Whatever moves the page body (a sheet that applies late, a font, the banner wrapping) changes | ||
| 2266 | + // the body's size, so it is measured again then too. | ||
| 2267 | + if (typeof ResizeObserver === 'function') { new ResizeObserver(queueShellTop).observe(document.body); } | ||
| 2250 | 2268 | ||
| 2251 | function syncRel() { | 2269 | function syncRel() { |
| 2252 | var now = document.documentElement.getAttribute('data-rel-now'); | 2270 | var now = document.documentElement.getAttribute('data-rel-now'); |