feat(sw): warn if failed to delete old cache
This commit is contained in:
parent
0d0b9d44eb
commit
410f43166c
2 changed files with 17 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,6 +2,7 @@
|
||||||
dist/
|
dist/
|
||||||
.output/
|
.output/
|
||||||
.astro/
|
.astro/
|
||||||
|
.continue/
|
||||||
|
|
||||||
# dependencies
|
# dependencies
|
||||||
node_modules/
|
node_modules/
|
||||||
|
|
17
src/sw.mjs
17
src/sw.mjs
|
@ -6,13 +6,28 @@
|
||||||
const cacheName = `${__prefix ?? 'app'}-v${__version ?? '000'}`
|
const cacheName = `${__prefix ?? 'app'}-v${__version ?? '000'}`
|
||||||
const forceLogging = true
|
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 cleanOldCaches = async () => {
|
||||||
const allowCacheNames = [cacheName]
|
const allowCacheNames = [cacheName]
|
||||||
const allCaches = await caches.keys()
|
const allCaches = await caches.keys()
|
||||||
allCaches.forEach((key) => {
|
allCaches.forEach((key) => {
|
||||||
if (!allowCacheNames.includes(key)) {
|
if (!allowCacheNames.includes(key)) {
|
||||||
console.info('Deleting old cache', 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)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue