From 4751ce9b80ac68df4b2768a768d79ec0b0c618f9 Mon Sep 17 00:00:00 2001 From: ayoayco Date: Sun, 18 Aug 2024 19:30:24 +0200 Subject: [PATCH] feat: delete old app assets caches; don't use date as version --- src/consts.ts | 3 ++- src/sw.mjs | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/consts.ts b/src/consts.ts index 16f2434..06db5b7 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -6,4 +6,5 @@ export const SITE_AUTHOR_MASTODON = 'https://social.ayco.io/@ayo'; export const SITE_PROJECT_REPO = 'https://github.com/ayoayco/Cozy'; export const SITE_DESCRIPTION = 'The Web is Yours.'; -export const VERSION = 'Quivering-Quacks'; \ No newline at end of file +export const VERSION = 'Raging-Rabits'; +// export const VERSION = 'Raging-Rabits-test-old'; \ No newline at end of file diff --git a/src/sw.mjs b/src/sw.mjs index 46849ba..169e4a1 100644 --- a/src/sw.mjs +++ b/src/sw.mjs @@ -4,9 +4,20 @@ import { logInfo } from './utils/logger' * -- find usage in `astro.config.mjs` integrations * @see https://ayco.io/n/@ayco/astro-sw */ -const cacheName = `${__prefix ?? 'app'}-v${__version + '-' + Date.now()}` +const cacheName = `${__prefix ?? 'app'}-v${__version ?? '000'}` const forceLogging = true; +const cleanOldCaches = async () => { + const allowCacheNames = ['cozy-reader', cacheName]; + const allCaches = await caches.keys(); + allCaches.forEach(key => { + if (!allowCacheNames.includes(key)) { + logInfo('Deleting old cache', {force: !!forceLogging, data: key}); + caches.delete(key); + } + }); +} + const addResourcesToCache = async (resources) => { const cache = await caches.open(cacheName); logInfo('adding resources to cache...', { force: !!forceLogging, context: 'cozy-sw', data: resources }) @@ -107,6 +118,7 @@ const enableNavigationPreload = async () => { self.addEventListener('activate', (event) => { logInfo('activating service worker...', { force: forceLogging, context: 'cozy-sw' }) + cleanOldCaches(); event.waitUntil(enableNavigationPreload()); });