// Page-wide cache for the GET requests the editors repeat (profile, palette, zones, presets, // groups, patterns). Any write (POST/PUT/PATCH/DELETE) empties it, entries expire after TTL_MS, // and a request with cache: 'no-store' always goes to the controller. Live data (/devices, // /status, /settings, ...) is never cached. (function () { const CACHEABLE = /^\/(profiles\/current|palettes|patterns|presets|zones|groups)(\/|\?|$)/; const TTL_MS = 30000; const store = new Map(); // url -> { at, promise } const nativeFetch = window.fetch.bind(window); const pathOf = (input) => { try { const u = new URL(typeof input === 'string' ? input : input.url, location.origin); return u.origin === location.origin ? u.pathname + u.search : null; } catch (_) { return null; } }; window.fetch = function (input, init) { const method = String((init && init.method) || (input && input.method) || 'GET').toUpperCase(); if (method !== 'GET') { store.clear(); return nativeFetch(input, init).finally(() => store.clear()); } const path = pathOf(input); const noStore = (init && init.cache === 'no-store') || (input && input.cache === 'no-store'); if (!path || noStore || !CACHEABLE.test(path)) { return nativeFetch(input, init); } const hit = store.get(path); if (hit && Date.now() - hit.at < TTL_MS) { return hit.promise.then((r) => r.clone()); } const promise = nativeFetch(input, init).then((r) => { if (!r.ok) store.delete(path); // never keep an error return r; }, (e) => { store.delete(path); throw e; }); store.set(path, { at: Date.now(), promise }); return promise.then((r) => r.clone()); }; })();