Files
romm/frontend/src/main.ts
Georges-Antoine Assi b475196327 fix(frontend): reload once when a stale chunk fails to load after a deploy
Hashed asset names change on every deploy, so a tab left open across an
upgrade 404s on its next lazy import and goes blank until the user
reloads by hand. Listen for `vite:preloadError` and for router
dynamic-import failures, and reload once. A session-scoped timestamp
guards against a reload loop when an asset is genuinely missing.

Fixes #4034
2026-08-01 14:12:41 -04:00

80 lines
2.3 KiB
TypeScript

import { createApp } from "vue";
import App from "@/RomM.vue";
import "@/console/index.css";
import { registerPlugins } from "@/plugins";
import router from "@/plugins/router";
import storeAuth from "@/stores/auth";
import storeConfig from "@/stores/config";
import storeHeartbeat from "@/stores/heartbeat";
import "@/styles/common.css";
import "@/styles/fonts.css";
import "@/styles/scrollbar.css";
import "@/v2/styles/global.css";
// Recover from stale chunks after a redeploy: hashed asset names change, so
// a tab opened before the deploy 404s (or gets HTML) on its next lazy import
// and the page goes blank. One reload picks up the new asset manifest; the
// timestamp guard avoids a reload loop when an asset is genuinely missing.
const CHUNK_RELOAD_KEY = "romm-chunk-reload-at";
function reloadOnceForStaleChunk(url?: string): boolean {
const lastReload = Number(sessionStorage.getItem(CHUNK_RELOAD_KEY) ?? 0);
if (Date.now() - lastReload < 60_000) return false;
sessionStorage.setItem(CHUNK_RELOAD_KEY, String(Date.now()));
if (url) {
window.location.assign(url);
} else {
window.location.reload();
}
return true;
}
function isChunkLoadError(error: unknown): boolean {
return (
error instanceof Error &&
/error loading dynamically imported module|failed to fetch dynamically imported module|importing a module script failed|unable to preload css/i.test(
error.message,
)
);
}
window.addEventListener("vite:preloadError", (event) => {
if (reloadOnceForStaleChunk()) event.preventDefault();
});
async function initializeData() {
const heartbeatStore = storeHeartbeat();
const authStore = storeAuth();
const configStore = storeConfig();
// Load initial data (config + heartbeat + user)
await Promise.all([
heartbeatStore.fetchHeartbeat(),
authStore.fetchCurrentUser(),
configStore.fetchConfig(),
]);
}
async function initializeApp() {
const app = createApp(App);
// Registrar vuetify + pinia + i18n + emitter
registerPlugins(app);
await initializeData();
// Route-level lazy imports fail outside vite's preload helper, so stale
// chunks during navigation surface here instead of as vite:preloadError.
router.onError((error, to) => {
if (isChunkLoadError(error)) {
reloadOnceForStaleChunk(to?.fullPath);
}
});
app.use(router);
app.mount("#app");
}
initializeApp();