feat: clean old caches via service worker
This commit is contained in:
parent
12c9e48da7
commit
3c432b3ed4
1 changed files with 12 additions and 0 deletions
12
src/sw.mjs
12
src/sw.mjs
|
@ -6,6 +6,17 @@
|
||||||
const cacheName = `${__prefix ?? 'app'}-v${__version ?? '000'}`
|
const cacheName = `${__prefix ?? 'app'}-v${__version ?? '000'}`
|
||||||
const forceLogging = true
|
const forceLogging = true
|
||||||
|
|
||||||
|
const cleanOldCaches = async () => {
|
||||||
|
const allowCacheNames = ['cozy-reader', cacheName]
|
||||||
|
const allCaches = await caches.keys()
|
||||||
|
allCaches.forEach((key) => {
|
||||||
|
if (!allowCacheNames.includes(key)) {
|
||||||
|
console.info('Deleting old cache', key)
|
||||||
|
caches.delete(key)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const addResourcesToCache = async (resources) => {
|
const addResourcesToCache = async (resources) => {
|
||||||
const cache = await caches.open(cacheName)
|
const cache = await caches.open(cacheName)
|
||||||
console.info('adding resources to cache...', {
|
console.info('adding resources to cache...', {
|
||||||
|
@ -73,6 +84,7 @@ const networkFirst = async ({ request, fallbackUrl }) => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
self.addEventListener('activate', (event) => {
|
self.addEventListener('activate', (event) => {
|
||||||
console.info('activating service worker...')
|
console.info('activating service worker...')
|
||||||
|
cleanOldCaches()
|
||||||
})
|
})
|
||||||
|
|
||||||
self.addEventListener('install', (event) => {
|
self.addEventListener('install', (event) => {
|
||||||
|
|
Loading…
Reference in a new issue