Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

chunkReloadGuard.ts

29 lines1.0 KB

1// Split chunks are fetched lazily, so a deploy (or desktop server swap) between
2// page load and a later fetch can 404 the old hashed assets. One reload picks
3// up the fresh index.html.
4const CHUNK_RELOAD_GUARD_KEY = "t3code:chunk-load-reloaded";
5​
6export function reloadOnceForChunkLoadError(
7 getStorage: () => Storage = () => window.sessionStorage,
8 reload: () => void = () => window.location.reload(),
9): boolean {
10 let alreadyReloaded: boolean;
11 try {
12 const storage = getStorage();
13 alreadyReloaded = storage.getItem(CHUNK_RELOAD_GUARD_KEY) === "1";
14 if (!alreadyReloaded) storage.setItem(CHUNK_RELOAD_GUARD_KEY, "1");
15 } catch {
16 // Without storage the guard cannot survive a reload, so a persistent
17 // failure would loop forever. Let the error surface instead.
18 return false;
19 }
20 if (alreadyReloaded) return false;
21 reload();
22 return true;
24​
25export function clearChunkReloadGuard(getStorage: () => Storage = () => window.sessionStorage) {
26 try {
27 getStorage().removeItem(CHUNK_RELOAD_GUARD_KEY);
28 } catch {}