fix(site): auto-reload on stale chunk after redeploy (#23575)

This commit is contained in:
Danielle Maywood
2026-03-25 12:49:51 +00:00
committed by GitHub
parent 07dbee69df
commit 12872be870
+16
View File
@@ -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");