From 88728db7228979b60dc4260b078603ea6775937f Mon Sep 17 00:00:00 2001 From: fly_mc <me@flymc.cc> Date: Fri, 25 Oct 2024 21:41:47 +0800 Subject: [PATCH] clear sw cache --- packages/sw/src/sw.ts | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/packages/sw/src/sw.ts b/packages/sw/src/sw.ts index 59fc7d8d5b..5c7d04752f 100644 --- a/packages/sw/src/sw.ts +++ b/packages/sw/src/sw.ts @@ -12,19 +12,40 @@ import { createEmptyNotification, createNotification } from '@/scripts/create-no import { swLang } from '@/scripts/lang.js'; import * as swos from '@/scripts/operations.js'; -globalThis.addEventListener('install', () => { - // ev.waitUntil(globalThis.skipWaiting()); -}); +const CACHE_NAME = `pari-cache-${_VERSION_}`; +const urlsToCache = [ + '/manifest.json', + '/assets', + '/emoji', + '/twemoji', + '/fluent-emoji', + '/vite', + '/identicon', + '/proxy' +]; +globalThis.addEventListener('install', async (event) => { + event.waitUntil( + caches.keys().then(cacheNames => { + return Promise.all( + cacheNames + .filter(name => name.startsWith('pari-cache-')) + .map(name => caches.delete(name)) + ); + }) + ); + await globalThis.skipWaiting(); +}); + globalThis.addEventListener('activate', ev => { ev.waitUntil( - caches.keys() - .then(cacheNames => Promise.all( - cacheNames - .filter((v) => v !== swLang.cacheName) - .map(name => caches.delete(name)), - )) - .then(() => globalThis.clients.claim()), + caches.keys() + .then(cacheNames => Promise.all( + cacheNames + .filter(name => name.startsWith('pari-cache-') || urlsToCache.some(url => name.includes(url))) + .map(name => caches.delete(name)) + )) + .then(() => globalThis.clients.claim()) ); });