From 9b2d6967330b85970b10f0781fec86bd815d42ad Mon Sep 17 00:00:00 2001 From: lifegpc Date: Mon, 12 Jun 2023 12:55:01 +0800 Subject: [PATCH] Update service worker --- static/sw.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/static/sw.ts b/static/sw.ts index 1592bbe..da2f303 100644 --- a/static/sw.ts +++ b/static/sw.ts @@ -8,6 +8,7 @@ async function get_deploy_id(): Promise { } let deploy_id: string | undefined = undefined; +let inited = false; const deleteCache = async (key: string) => { await caches.delete(key); @@ -15,6 +16,7 @@ const deleteCache = async (key: string) => { const deleteOldCaches = async () => { deploy_id = await get_deploy_id(); + inited = true; const keyList = await caches.keys(); const cachesToDelete = keyList.filter((key) => key !== deploy_id); await Promise.all(cachesToDelete.map(deleteCache)); @@ -40,6 +42,7 @@ function match_url(u: URL) { /**@ts-ignore */ self.addEventListener("fetch", async (e: FetchEvent) => { + if (!inited) await deleteOldCaches(); const r = e.request; const responseFromCache = await caches.match(r); if (responseFromCache) {