mirror of
https://github.com/cheeaun/phanpy.git
synced 2025-03-07 08:37:31 +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
42
src/main.jsx
42
src/main.jsx
|
@ -50,26 +50,34 @@ 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 = 30;
|
const MAX_SW_CACHE_SIZE = 50;
|
||||||
const IGNORE_CACHE_KEYS = ['icons'];
|
const IGNORE_CACHE_KEYS = ['icons'];
|
||||||
const clearCaches = async () => {
|
let clearedOnce = false;
|
||||||
if (!window.__IDLE__) return;
|
const FAST_INTERVAL = 10_000; // 10 seconds
|
||||||
const keys = await caches.keys();
|
const SLOW_INTERVAL = 60 * 60 * 1000; // 1 hour
|
||||||
for (const key of keys) {
|
async function clearCaches() {
|
||||||
if (IGNORE_CACHE_KEYS.includes(key)) continue;
|
if (window.__IDLE__) {
|
||||||
const cache = await caches.open(key);
|
try {
|
||||||
const _keys = await cache.keys();
|
const keys = await caches.keys();
|
||||||
if (_keys.length > MAX_SW_CACHE_SIZE) {
|
for (const key of keys) {
|
||||||
console.warn('Cleaning cache', key, _keys.length);
|
if (IGNORE_CACHE_KEYS.includes(key)) continue;
|
||||||
const deleteKeys = _keys.slice(MAX_SW_CACHE_SIZE);
|
const cache = await caches.open(key);
|
||||||
for (const deleteKey of deleteKeys) {
|
const _keys = await cache.keys();
|
||||||
await cache.delete(deleteKey);
|
if (_keys.length > MAX_SW_CACHE_SIZE) {
|
||||||
|
console.warn('Cleaning cache', key, _keys.length);
|
||||||
|
const deleteKeys = _keys.slice(MAX_SW_CACHE_SIZE);
|
||||||
|
for (const deleteKey of deleteKeys) {
|
||||||
|
await cache.delete(deleteKey);
|
||||||
|
}
|
||||||
|
clearedOnce = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
} catch (e) {} // Silent fail
|
||||||
}
|
}
|
||||||
};
|
// Once cleared, clear again at slower interval
|
||||||
setTimeout(clearCaches, 10_000); // after 10 seconds
|
setTimeout(clearCaches, clearedOnce ? SLOW_INTERVAL : FAST_INTERVAL);
|
||||||
setInterval(clearCaches, 30 * 60 * 1000); // every 30 minutes
|
}
|
||||||
|
setTimeout(clearCaches, FAST_INTERVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
window.__CLOAK__ = () => {
|
window.__CLOAK__ = () => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue