After a deploy, the first in-page navigation no longer collapses the repo tabs and the CI runs list: the old stylesheet stays until the new one has loaded, and the layout is measured again once it applies
Browse files at this commit90755a5ce9fc7245288881c54164f01ae71b3862Parent: 5219076- fix
- after a deploy, the first in-page navigation no longer collapses the repo tabs and the CI runs list: the old stylesheet stays until the new one has loaded, and the layout is measured again once it applies
- a CI badge (in a README too) is fetched again on every in-page navigation, so it no longer shows a finished run as running until a reload
1 changed file +36 -5
crates/forge-web/assets/nav.jsmodified+36 -5
| @@ -125,23 +125,38 @@ | |||
| 125 | } | 125 | } |
| 126 | 126 | ||
| 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. | 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 | ||
| 130 | + // unstyled, and the layout measured from it (--shell-top) is taken again once it applies. | ||
| 129 | function syncStyles(doc) { | 131 | function syncStyles(doc) { |
| 130 | var sheets = 'link[rel="stylesheet"]'; | 132 | var sheets = 'link[rel="stylesheet"]'; |
| 131 | var wanted = {}; | 133 | var wanted = {}; |
| 132 | doc.head.querySelectorAll(sheets).forEach(function (l) { wanted[l.getAttribute('href') || ''] = true; }); | 134 | doc.head.querySelectorAll(sheets).forEach(function (l) { wanted[l.getAttribute('href') || ''] = true; }); |
| 133 | var have = {}; | 135 | var have = {}; |
| 134 | document.head.querySelectorAll(sheets).forEach(function (l) { have[l.getAttribute('href') || ''] = true; }); | 136 | document.head.querySelectorAll(sheets).forEach(function (l) { have[l.getAttribute('href') || ''] = true; }); |
| 137 | + var pending = 0; | ||
| 138 | + function dropExtras() { | ||
| 139 | + document.head.querySelectorAll(sheets).forEach(function (l) { | ||
| 140 | + if (!wanted[l.getAttribute('href') || '']) { l.remove(); } | ||
| 141 | + }); | ||
| 142 | + queueShellTop(); | ||
| 143 | + } | ||
| 144 | + function settled() { | ||
| 145 | + pending -= 1; | ||
| 146 | + if (pending === 0) { dropExtras(); } | ||
| 147 | + } | ||
| 135 | doc.head.querySelectorAll(sheets).forEach(function (l) { | 148 | doc.head.querySelectorAll(sheets).forEach(function (l) { |
| 136 | var href = l.getAttribute('href') || ''; | 149 | var href = l.getAttribute('href') || ''; |
| 137 | if (!have[href]) { | 150 | if (!have[href]) { |
| 138 | have[href] = true; | 151 | have[href] = true; |
| 139 | - document.head.appendChild(document.importNode(l, true)); | 152 | + var link = document.importNode(l, true); |
| 153 | + pending += 1; | ||
| 154 | + link.addEventListener('load', settled, { once: true }); | ||
| 155 | + link.addEventListener('error', settled, { once: true }); | ||
| 156 | + document.head.appendChild(link); | ||
| 140 | } | 157 | } |
| 141 | }); | 158 | }); |
| 142 | - document.head.querySelectorAll(sheets).forEach(function (l) { | 159 | + if (pending === 0) { dropExtras(); } |
| 143 | - if (!wanted[l.getAttribute('href') || '']) { l.remove(); } | ||
| 144 | - }); | ||
| 145 | } | 160 | } |
| 146 | 161 | ||
| 147 | function adopt(doc, keepFields, dd, jobs) { | 162 | function adopt(doc, keepFields, dd, jobs) { |
| @@ -176,6 +191,7 @@ | |||
| 176 | syncRel(); | 191 | syncRel(); |
| 177 | syncBannerExpiry(); | 192 | syncBannerExpiry(); |
| 178 | syncShellTop(); | 193 | syncShellTop(); |
| 194 | + syncBadges(); | ||
| 179 | syncDrafts(); | 195 | syncDrafts(); |
| 180 | } | 196 | } |
| 181 | 197 | ||
| @@ -1960,6 +1976,21 @@ | |||
| 1960 | document.documentElement.style.setProperty('--shell-top', Math.round(shell.getBoundingClientRect().top) + 'px'); | 1976 | document.documentElement.style.setProperty('--shell-top', Math.round(shell.getBoundingClientRect().top) + 'px'); |
| 1961 | } | 1977 | } |
| 1962 | 1978 | ||
| 1979 | + // A CI badge (`/{owner}/{repo}/badge.svg`, often in a README) is fetched again after a swap. The | ||
| 1980 | + // document stays the same across a soft navigation, and the browser reuses an image it already | ||
| 1981 | + // has at the same URL whatever its Cache-Control says, so the badge would keep an old state until | ||
| 1982 | + // a reload. The badge ignores query keys it does not know, so `_r` only makes the URL new. | ||
| 1983 | + function syncBadges() { | ||
| 1984 | + var stamp = String(Date.now()); | ||
| 1985 | + document.querySelectorAll('img[src*="/badge.svg"]').forEach(function (img) { | ||
| 1986 | + var url; | ||
| 1987 | + try { url = new URL(img.getAttribute('src'), location.href); } catch (err) { return; } | ||
| 1988 | + if (url.origin !== location.origin || !/\/badge\.svg$/.test(url.pathname)) { return; } | ||
| 1989 | + url.searchParams.set('_r', stamp); | ||
| 1990 | + img.src = url.pathname + url.search; | ||
| 1991 | + }); | ||
| 1992 | + } | ||
| 1993 | + | ||
| 1963 | function queueShellTop() { | 1994 | function queueShellTop() { |
| 1964 | if (!shellFrame) { shellFrame = requestAnimationFrame(syncShellTop); } | 1995 | if (!shellFrame) { shellFrame = requestAnimationFrame(syncShellTop); } |
| 1965 | } | 1996 | } |