feat(sw): warn if failed to delete old cache

This commit is contained in:
Ayo Ayco 2025-08-26 00:19:54 +02:00
parent 0d0b9d44eb
commit 410f43166c
2 changed files with 17 additions and 1 deletions

1
.gitignore vendored
View file

@ -2,6 +2,7 @@
dist/
.output/
.astro/
.continue/
# dependencies
node_modules/

View file

@ -6,13 +6,28 @@
const cacheName = `${__prefix ?? 'app'}-v${__version ?? '000'}`
const forceLogging = true
/**
* Cleans up old service worker caches by deleting any cache that doesn't match the current cache name.
* This ensures only the current version of the application's cache is retained.
* @async
* @function cleanOldCaches
* @returns {Promise<void>} A promise that resolves when old caches have been deleted
*/
const cleanOldCaches = async () => {
const allowCacheNames = [cacheName]
const allCaches = await caches.keys()
allCaches.forEach((key) => {
if (!allowCacheNames.includes(key)) {
console.info('Deleting old cache', key)
caches.delete(key)
caches
.delete(key)
.then(() => {
console.info('Successfully deleted cache:', key)
})
.catch((error) => {
console.warn('Failed to delete old cache:', key, error)
})
}
})
}