feat(pwa): ✨ clear old cache on sw activate
This commit is contained in:
parent
6d29321e88
commit
838bdec6ea
|
|
@ -84,8 +84,9 @@ if (IS_SW) {
|
||||||
event.waitUntil(openPreCache())
|
event.waitUntil(openPreCache())
|
||||||
})
|
})
|
||||||
|
|
||||||
self.addEventListener('activate', () => {
|
self.addEventListener('activate', (event) => {
|
||||||
//TODO handle activation
|
// Clear old caches to reduce storage usage
|
||||||
|
event.waitUntil(clearOldCaches())
|
||||||
})
|
})
|
||||||
|
|
||||||
self.addEventListener('fetch', (event) => {
|
self.addEventListener('fetch', (event) => {
|
||||||
|
|
@ -252,6 +253,23 @@ async function openPreCache() {
|
||||||
return { cache, version, preCachedUrls }
|
return { cache, version, preCachedUrls }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function clearOldCaches() {
|
||||||
|
// Clear memory
|
||||||
|
memory.clear()
|
||||||
|
|
||||||
|
// List all caches by their names.
|
||||||
|
const names = await caches.keys()
|
||||||
|
const preCacheName = await swStorage.getItem('$sw.pre-cache.version')
|
||||||
|
const dynCacheName = await swStorage.getItem('$sw.dyn-cache.version')
|
||||||
|
|
||||||
|
for (const name of names) {
|
||||||
|
// If a cache's name is the current name, delete it.
|
||||||
|
if (name === preCacheName) continue
|
||||||
|
if (name == dynCacheName) continue
|
||||||
|
caches.delete(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
console.assert()
|
console.assert()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue