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

chunkReloadGuard.ts

26 lines899 Bytes

1// Deploys and desktop server swaps can invalidate lazy chunks; one reload fetches the fresh index.
2const CHUNK_RELOAD_GUARD_KEY = "t3code:chunk-load-reloaded";
3​
4export function reloadOnceForChunkLoadError(
5 getStorage: () => Storage = () => window.sessionStorage,
6 reload: () => void = () => window.location.reload(),
7): boolean {
8 let alreadyReloaded: boolean;
9 try {
10 const storage = getStorage();
11 alreadyReloaded = storage.getItem(CHUNK_RELOAD_GUARD_KEY) === "1";
12 if (!alreadyReloaded) storage.setItem(CHUNK_RELOAD_GUARD_KEY, "1");
13 } catch {
14 // Without storage, the reload guard cannot prevent an infinite loop.
15 return false;
16 }
17 if (alreadyReloaded) return false;
18 reload();
19 return true;
21​
22export function clearChunkReloadGuard(getStorage: () => Storage = () => window.sessionStorage) {
23 try {
24 getStorage().removeItem(CHUNK_RELOAD_GUARD_KEY);
25 } catch {}