From c7bc698810e07c40dfab58839559c20092ea8882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=AB=E8=B0=B7=E5=89=91=E4=BB=99?= Date: Thu, 14 Jul 2022 16:19:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=8D=E6=AC=A1=E4=BF=AE=E8=AE=A2sw?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service-worker.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/service-worker.js b/service-worker.js index b410437b..cd8fdae4 100644 --- a/service-worker.js +++ b/service-worker.js @@ -6526,6 +6526,8 @@ self.addEventListener('install', function (event) { self.addEventListener('activate', function(event) { console.debug("Service Worker activate"); + const baseUrl = new URL(".", location); + const baseUrlString = baseUrl.origin + baseUrl.pathname; event.waitUntil( Promise.all([ @@ -6543,13 +6545,11 @@ self.addEventListener('activate', function(event) { return Promise.all( requests.map(async function(request) { const url = new URL(request.url); - const baseUrl = new URL(".", location); - const path = url.pathname; - const relativePath = (url.origin + path).replace(baseUrl.origin + baseUrl.pathname, ""); + const relativePath = (url.origin + url.pathname).replace(baseUrlString, ""); const oldmd5 = url.searchParams.get("md5"); //老的md5值 - const md5 = cachesMap.get(relativePath); //记录的md5值 + const md5 = cachesMap.get(relativePath); //新的md5值 //console.debug("检测缓存条目", url, relativePath, oldmd5, md5); - if (!md5 || md5 !== oldmd5) { + if (md5 && md5 !== oldmd5) { console.debug("删除版本不匹配的缓存", request); return cache.delete(request); } @@ -6567,8 +6567,8 @@ self.addEventListener('fetch', async function(event) { const url = new URL(event.request.url); url.search = ""; const baseUrl = new URL(".", location); - const path = url.pathname; - const relativePath = (url.origin + path).replace(baseUrl.origin + baseUrl.pathname, ""); + const baseUrlString = baseUrl.origin + baseUrl.pathname; + const relativePath = (url.origin + url.pathname).replace(baseUrlString, ""); const md5 = cachesMap.get(relativePath); //事先记录的md5值 //console.debug("请求网络", event.request.url, relativePath, md5); if (md5) { @@ -6585,7 +6585,7 @@ self.addEventListener('fetch', async function(event) { cache.put(url, newResponse.clone()); event.respondWith(newResponse); } catch (error) { - console.error("%c数据在线获取失败,尝试使用忽略 search 的离线数据", "color: red;", error); + console.error("%c数据在线获取失败,尝试使用忽略 search 的离线数据", "color: red;", url, error); event.respondWith(caches.match(url, {ignoreSearch: true})); } } @@ -6597,7 +6597,7 @@ self.addEventListener('fetch', async function(event) { cache.put(url, newResponse.clone()); event.respondWith(newResponse); } catch (error) { - console.error("%c数据在线获取失败,尝试使用忽略 search 的离线数据", "color: red;", error); + console.error("%c数据在线获取失败,尝试使用忽略 search 的离线数据", "color: red;", url, error); event.respondWith(caches.match(url, {ignoreSearch: true})); } }