Double-down on reducing cache size

This commit is contained in:
Lim Chee Aun 2024-09-18 12:19:50 +08:00
parent 15732dc466
commit 73a4326b55

View file

@ -50,13 +50,13 @@ setTimeout(() => {
// Service worker cache cleanup // Service worker cache cleanup
if ('serviceWorker' in navigator && typeof caches !== 'undefined') { if ('serviceWorker' in navigator && typeof caches !== 'undefined') {
const MAX_SW_CACHE_SIZE = 300; const MAX_SW_CACHE_SIZE = 30;
let swInterval = setInterval(() => { const IGNORE_CACHE_KEYS = ['icons'];
if (window.__IDLE__) { const clearCaches = async () => {
clearInterval(swInterval); if (!window.__IDLE__) return;
(async () => {
const keys = await caches.keys(); const keys = await caches.keys();
for (const key of keys) { for (const key of keys) {
if (IGNORE_CACHE_KEYS.includes(key)) continue;
const cache = await caches.open(key); const cache = await caches.open(key);
const _keys = await cache.keys(); const _keys = await cache.keys();
if (_keys.length > MAX_SW_CACHE_SIZE) { if (_keys.length > MAX_SW_CACHE_SIZE) {
@ -67,9 +67,9 @@ if ('serviceWorker' in navigator && typeof caches !== 'undefined') {
} }
} }
} }
})(); };
} setTimeout(clearCaches, 10_000); // after 10 seconds
}, 15_000); setInterval(clearCaches, 30 * 60 * 1000); // every 30 minutes
} }
window.__CLOAK__ = () => { window.__CLOAK__ = () => {