feat: delete old app assets caches; don't use date as version

This commit is contained in:
Ayo Ayco 2024-08-18 19:30:24 +02:00
parent 91791b37b5
commit 4751ce9b80
2 changed files with 15 additions and 2 deletions

View file

@ -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';
export const VERSION = 'Raging-Rabits';
// export const VERSION = 'Raging-Rabits-test-old';

View file

@ -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());
});