mirror of
https://github.com/cheeaun/phanpy.git
synced 2024-11-24 10:15:37 +03:00
Try this solution to clean up excess SW cache
This commit is contained in:
parent
f3345ad9f8
commit
1d949a1ab2
1 changed files with 24 additions and 0 deletions
24
src/main.jsx
24
src/main.jsx
|
@ -48,6 +48,30 @@ setTimeout(() => {
|
|||
} catch (e) {}
|
||||
}, 5000);
|
||||
|
||||
// Service worker cache cleanup
|
||||
if ('serviceWorker' in navigator && typeof caches !== 'undefined') {
|
||||
const MAX_SW_CACHE_SIZE = 300;
|
||||
let swInterval = setInterval(() => {
|
||||
if (window.__IDLE__) {
|
||||
clearInterval(swInterval);
|
||||
(async () => {
|
||||
const keys = await caches.keys();
|
||||
for (const key of keys) {
|
||||
const cache = await caches.open(key);
|
||||
const _keys = await cache.keys();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
})();
|
||||
}
|
||||
}, 15_000);
|
||||
}
|
||||
|
||||
window.__CLOAK__ = () => {
|
||||
document.body.classList.toggle('cloak');
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue