Co-authored-by: Claude Fable 5 <[email protected]> Co-authored-by: Theo Browne <[email protected]>
40 lines
1.6 KiB
TypeScript
40 lines
1.6 KiB
TypeScript
// Split chunks are fetched lazily, so a deploy (or desktop server swap)
|
|
// between page load and a later fetch can 404 the old hashed assets. One
|
|
// reload picks up the fresh index.html. A sessionStorage flag keeps a
|
|
// persistent failure from becoming a reload loop, and a successful boot clears
|
|
// it so the next stale deploy gets its own single reload.
|
|
const CHUNK_RELOAD_GUARD_KEY = "t3code:chunk-load-reloaded";
|
|
|
|
/**
|
|
* Called from the `vite:preloadError` listener. Reloads at most once per
|
|
* failure streak and returns whether it did, so the caller knows whether to
|
|
* swallow the event or let the error surface through the normal paths.
|
|
*/
|
|
export function reloadOnceForChunkLoadError(
|
|
getStorage: () => Storage = () => window.sessionStorage,
|
|
reload: () => void = () => window.location.reload(),
|
|
): boolean {
|
|
let alreadyReloaded: boolean;
|
|
try {
|
|
const storage = getStorage();
|
|
alreadyReloaded = storage.getItem(CHUNK_RELOAD_GUARD_KEY) === "1";
|
|
if (!alreadyReloaded) storage.setItem(CHUNK_RELOAD_GUARD_KEY, "1");
|
|
} catch {
|
|
// Without storage the guard cannot survive a reload, so a persistent
|
|
// failure would loop forever. Let the error surface instead.
|
|
return false;
|
|
}
|
|
if (alreadyReloaded) return false;
|
|
reload();
|
|
return true;
|
|
}
|
|
|
|
/** Clears the guard after a successful boot so a later stale deploy can reload again. */
|
|
export function clearChunkReloadGuard(getStorage: () => Storage = () => window.sessionStorage) {
|
|
try {
|
|
getStorage().removeItem(CHUNK_RELOAD_GUARD_KEY);
|
|
} catch {
|
|
// Blocked storage never held the flag.
|
|
}
|
|
}
|