feat: clean old caches via service worker

This commit is contained in:
Ayo Ayco 2025-04-05 09:33:50 +02:00
parent 12c9e48da7
commit 3c432b3ed4

View file

@ -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) => {