Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): stop a failing nested chunk from reloading forever
Yudhi Armyndharis05467c6a9f08
diff --git a/dashboard/src/utils/chunkReload.test.ts b/dashboard/src/utils/chunkReload.test.tsindex 9b734636c..10c1fccb9 100644--- a/dashboard/src/utils/chunkReload.test.ts+++ b/dashboard/src/utils/chunkReload.test.ts@@ -2,19 +2,18 @@ import { test } from 'node:test';import assert from 'node:assert/strict';import { loadChunkWithReload } from './chunkReload.ts';-function makeStorage(initial: Record<string, string> = {}): Pick<Storage, 'getItem' | 'setItem' | 'removeItem'> {+function makeStorage(initial: Record<string, string> = {}): Pick<Storage, 'getItem' | 'setItem'> {const m = new Map<string, string>(Object.entries(initial));return {getItem: k => m.get(k) ?? null,setItem: (k, v) => void m.set(k, v),- removeItem: k => void m.delete(k),};}const flush = () => new Promise(resolve => setTimeout(resolve, 0));-test('returns the module and clears the reload flag on success', async () => {- const storage = makeStorage({ owa_chunk_reloaded: '1' });+test('returns the module on success without reloading', async () => {+ const storage = makeStorage();const mod = { default: 'Component' };let reloads = 0;@@ -22,7 +21,6 @@ test('returns the module and clears the reload flag on success', async () => {assert.equal(result, mod);assert.equal(reloads, 0);- assert.equal(storage.getItem('owa_chunk_reloaded'), null);});test('reloads exactly once on a chunk failure when no reload has happened yet', async () => {@@ -37,11 +35,11 @@ test('reloads exactly once on a chunk failure when no reload has happened yet',await flush();assert.equal(reloads, 1);- assert.equal(storage.getItem('owa_chunk_reloaded'), '1');+ assert.ok(Date.now() - Number(storage.getItem('owa_chunk_reloaded')) < 1000);});-test('rethrows instead of reloading again once a reload already happened (no loop)', async () => {- const storage = makeStorage({ owa_chunk_reloaded: '1' });+test('rethrows instead of reloading again when a reload just happened (no loop)', async () => {+ const storage = makeStorage({ owa_chunk_reloaded: String(Date.now()) });let reloads = 0;await assert.rejects(@@ -50,3 +48,47 @@ test('rethrows instead of reloading again once a reload already happened (no loo);assert.equal(reloads, 0);});++// A page load where the route chunk loads and a nested lazy chunk (DashboardCharts) keeps failing.+// The route chunk's success must not re-arm the guard, or every load reloads the page again.+test('a successful chunk in the same load does not re-arm the reload for a failing one', async () => {+ const storage = makeStorage();+ let reloads = 0;+ const deps = { reload: () => reloads++, storage };+ const failing = () => Promise.reject(new Error('blocked'));++ void loadChunkWithReload(failing, deps);+ await flush();+ assert.equal(reloads, 1);++ // The next page load after that reload.+ await loadChunkWithReload(() => Promise.resolve({ default: 'Dashboard' }), deps);+ await assert.rejects(loadChunkWithReload(failing, deps), /blocked/);+ assert.equal(reloads, 1);+});++test('reloads again for a failure well after the last reload (a later redeploy)', async () => {+ const storage = makeStorage({ owa_chunk_reloaded: String(Date.now() - 60 * 60 * 1000) });+ let reloads = 0;++ void loadChunkWithReload(() => Promise.reject(new Error('Loading chunk 9 failed')), {+ reload: () => reloads++,+ storage,+ });+ await flush();++ assert.equal(reloads, 1);+});++test('a flag left by an older build still allows the first reload', async () => {+ const storage = makeStorage({ owa_chunk_reloaded: '1' });+ let reloads = 0;++ void loadChunkWithReload(() => Promise.reject(new Error('Loading chunk 3 failed')), {+ reload: () => reloads++,+ storage,+ });+ await flush();++ assert.equal(reloads, 1);+});diff --git a/dashboard/src/utils/chunkReload.ts b/dashboard/src/utils/chunkReload.tsindex 49b218600..068428982 100644--- a/dashboard/src/utils/chunkReload.ts+++ b/dashboard/src/utils/chunkReload.ts@@ -1,29 +1,35 @@// Recovery for a failed dynamic import() of a route/lazy chunk. The dominant cause is a redeploy:// the running index.html references hashed chunk filenames that no longer exist on the server, so-// import() rejects. A one-time full reload pulls the fresh index + chunks. A sessionStorage flag-// guards against a reload loop when the failure is not deploy-related (adblock, offline, real 404).+// import() rejects. A one-time full reload pulls the fresh index + chunks. A sessionStorage timestamp+// guards against a reload loop when the failure is not deploy-related (adblock, offline, real 404):+// a failure within RELOAD_WINDOW_MS of the last reload is surfaced instead of reloading again.// React-free on purpose so it is unit-testable without a DOM. See lazyWithRetry.ts for the wiring.const RELOAD_KEY = 'owa_chunk_reloaded';+// Long enough to cover a slow reload reaching the same failing chunk again, short enough that a later+// redeploy in the same tab still gets its reload. The guard is not cleared on success: a nested lazy+// chunk that keeps failing after its route chunk loaded would otherwise reload on every page load.+const RELOAD_WINDOW_MS = 30_000;+export interface ChunkReloadDeps {reload: () => void;- storage: Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>;+ storage: Pick<Storage, 'getItem' | 'setItem'>;}export async function loadChunkWithReload<T>(factory: () => Promise<T>, deps: ChunkReloadDeps): Promise<T> {try {- const mod = await factory();- deps.storage.removeItem(RELOAD_KEY);- return mod;+ return await factory();} catch (err) {- if (!deps.storage.getItem(RELOAD_KEY)) {- deps.storage.setItem(RELOAD_KEY, '1');+ // A missing key reads as 0 and a legacy '1' flag as long ago, so both allow the first reload.+ const lastReload = Number(deps.storage.getItem(RELOAD_KEY)) || 0;+ if (Date.now() - lastReload >= RELOAD_WINDOW_MS) {+ deps.storage.setItem(RELOAD_KEY, String(Date.now()));deps.reload();// Hold Suspense until the reload navigates away; never resolve/reject this load.return new Promise<T>(() => {});}- // A reload already happened and it still failed → let the caller's error boundary surface it.+ // A reload just happened and it still failed → let the caller's error boundary surface it.throw err;}}