mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix(site): auto-reload on stale chunk after redeploy (#23575)
This commit is contained in:
@@ -11,6 +11,22 @@ console.info(` -####### +######- ########+ ##########
|
||||
.########+ -########. #########+ ########## #### .#### ###########
|
||||
`);
|
||||
|
||||
// After a redeploy, the SPA still holds old chunk filenames in memory.
|
||||
// Navigating to a lazy-loaded route will try to fetch a chunk that no
|
||||
// longer exists (the content hash changed), crashing the app. Vite fires
|
||||
// this event whenever a dynamic-import preload fails. We catch it and
|
||||
// silently reload so the browser fetches a fresh index.html with the new
|
||||
// chunk names. A sessionStorage guard prevents infinite reload loops.
|
||||
window.addEventListener("vite:preloadError", () => {
|
||||
const key = "preload-reload";
|
||||
const last = sessionStorage.getItem(key);
|
||||
const now = Date.now();
|
||||
if (!last || now - Number(last) > 10_000) {
|
||||
sessionStorage.setItem(key, String(now));
|
||||
window.location.reload();
|
||||
}
|
||||
});
|
||||
|
||||
const element = document.getElementById("root");
|
||||
if (element === null) {
|
||||
throw new Error("root element is null");
|
||||
|
||||
Reference in New Issue
Block a user