Files
romm/frontend/src/main.ts
Sam Dornan 3ebe5e7541 fix(i18n): translate route titles once locale messages are loaded
Opening a route in a new tab showed the raw i18n key (e.g.
"settings.scan-settings") as the tab title. Locale namespaces load as
separate chunks and nothing awaited them, so the router guard resolved
the title before any messages existed. Route definitions compounded it
by calling `t()` at module-eval time, baking the key into `meta.title`
and leaving the guard to translate it a second time.

Bootstrap now awaits a `localesReady` promise before installing the
router, `meta.title` holds the plain key, and a locale watcher
re-translates the current title — the stored language is applied when
the app mounts, after the first navigation guard has already run.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-18 17:05:58 -05:00

84 lines
2.6 KiB
TypeScript

import { createApp } from "vue";
import App from "@/RomM.vue";
import "@/console/index.css";
import { localesReady } from "@/locales";
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);
// Locale messages gate the router alongside the initial data: the guard
// resolves the route title as soon as the router is installed, and with
// messages still in flight it would set the raw key as the tab title.
await Promise.all([initializeData(), localesReady]);
// 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();