mirror of
https://github.com/cheeaun/phanpy.git
synced 2024-12-18 07:12:52 +03:00
Make sure cleanup runs at least once
This commit is contained in:
parent
73a4326b55
commit
0e3cc864db
1 changed files with 25 additions and 17 deletions
20
src/main.jsx
20
src/main.jsx
|
@ -50,10 +50,14 @@ setTimeout(() => {
|
|||
|
||||
// Service worker cache cleanup
|
||||
if ('serviceWorker' in navigator && typeof caches !== 'undefined') {
|
||||
const MAX_SW_CACHE_SIZE = 30;
|
||||
const MAX_SW_CACHE_SIZE = 50;
|
||||
const IGNORE_CACHE_KEYS = ['icons'];
|
||||
const clearCaches = async () => {
|
||||
if (!window.__IDLE__) return;
|
||||
let clearedOnce = false;
|
||||
const FAST_INTERVAL = 10_000; // 10 seconds
|
||||
const SLOW_INTERVAL = 60 * 60 * 1000; // 1 hour
|
||||
async function clearCaches() {
|
||||
if (window.__IDLE__) {
|
||||
try {
|
||||
const keys = await caches.keys();
|
||||
for (const key of keys) {
|
||||
if (IGNORE_CACHE_KEYS.includes(key)) continue;
|
||||
|
@ -65,11 +69,15 @@ if ('serviceWorker' in navigator && typeof caches !== 'undefined') {
|
|||
for (const deleteKey of deleteKeys) {
|
||||
await cache.delete(deleteKey);
|
||||
}
|
||||
clearedOnce = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
setTimeout(clearCaches, 10_000); // after 10 seconds
|
||||
setInterval(clearCaches, 30 * 60 * 1000); // every 30 minutes
|
||||
} catch (e) {} // Silent fail
|
||||
}
|
||||
// Once cleared, clear again at slower interval
|
||||
setTimeout(clearCaches, clearedOnce ? SLOW_INTERVAL : FAST_INTERVAL);
|
||||
}
|
||||
setTimeout(clearCaches, FAST_INTERVAL);
|
||||
}
|
||||
|
||||
window.__CLOAK__ = () => {
|
||||
|
|
Loading…
Reference in a new issue