From 828ffeb8427ae58f887f8ebe000f6b033ce11ed3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=AB=E8=B0=B7=E5=89=91=E4=BB=99?= Date: Sun, 19 Sep 2021 03:30:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=BC=E4=B9=8E=E6=98=AF=E5=B7=B2=E7=BB=8F?= =?UTF-8?q?=E6=8A=8A=E6=89=80=E6=9C=89=E7=9A=84=E5=86=85=E5=AE=B9=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E8=BF=9B=E5=8E=BB=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/export-player-data.html | 14 ++++- doc/index.html | 14 ++++- run http-server.bat | 2 + service-worker.js | 101 ++++++++++++++++++++++++------------ 4 files changed, 96 insertions(+), 35 deletions(-) create mode 100644 run http-server.bat diff --git a/doc/export-player-data.html b/doc/export-player-data.html index ef444b1b..4c04c34f 100644 --- a/doc/export-player-data.html +++ b/doc/export-player-data.html @@ -4,7 +4,7 @@ 智龙急速阵型帮助 - + + diff --git a/doc/index.html b/doc/index.html index df1f232f..8d29bdd1 100644 --- a/doc/index.html +++ b/doc/index.html @@ -4,7 +4,7 @@ 智龙急速阵型帮助 - + + diff --git a/run http-server.bat b/run http-server.bat new file mode 100644 index 00000000..9d5f03c2 --- /dev/null +++ b/run http-server.bat @@ -0,0 +1,2 @@ +@echo off +http-server -p 80 \ No newline at end of file diff --git a/service-worker.js b/service-worker.js index f62dbb71..a40768cf 100644 --- a/service-worker.js +++ b/service-worker.js @@ -1,4 +1,7 @@ const CACHES = new Map([ + ['program', 'program-cache-v1'], + ['data', 'data-cache-v1'], + ['document', 'document-cache-v1'], ['font', 'font-cache-v1'], ['cards_ja', 'card_ja-cache-v1'], ['cards_en', 'card_en-cache-v1'], @@ -9,17 +12,29 @@ ]); self.addEventListener('install', function(event) { - console.debug("安装中", ENV); - self.skipWaiting(); - event.waitUntil( - cache.open(CACHES.get("font")).then(function(cache) { - return cache.addAll([ - '/PADDashFormation/fonts/fa-solid-900.woff2', - '/PADDashFormation/fonts/FOT-KurokaneStd-EB.woff2', - '/PADDashFormation/fonts/zpix.woff2', - ]); - }) - ); + console.debug("Service Worker 安装中"); + const preCache = () => { + return Promise.all([ + cache.open(CACHES.get("font")).then(function(cache) { + return cache.addAll([ + 'fonts/fa-solid-900.woff2', + 'fonts/FOT-KurokaneStd-EB.woff2', + 'fonts/zpix.woff2', + ]); + }), + cache.open(CACHES.get("library")).then(function(cache) { + return cache.addAll([ + 'library/html2canvas.min.js', + 'library/zxing.umd.min.js', + 'library/jy4340132-aaa/adpcm.js', + 'library/jy4340132-aaa/adpcm.wasm', + 'library/jy4340132-aaa/pcm_player.js', + 'library/jy4340132-aaa/std.js', + ]); + }), + ]); + }; + event.waitUntil(preCache()); }); self.addEventListener('activate', function(event) { @@ -40,28 +55,48 @@ self.addEventListener('activate', function(event) { self.addEventListener('fetch', function(event) { event.respondWith( caches.match(event.request).then(function(resp) { - if (resp) console.debug("找到缓存", event.request.url); - return resp || fetch(event.request).then(async function(response) { - //console.debug("正则测试",/images\/cards\w+\/CARDS_\d+\.PNG/i.test(event.request.url)); - if (event.request.url.includes(".woff2")) { //缓存字体 - console.debug("缓存字体", event.request.url); - const cache = await caches.open(CACHES.get("font")); - cache.put(event.request, response.clone()); - } else if (/images\/cards_\w+\/CARDS_\d+\.PNG/i.test(event.request.url)) { //缓存卡片图 - let regRes = /cards_(ja|en|ko)/i.exec(event.request.url); - let langCode = regRes[1]; - console.debug("缓存Cards-" + langCode, event.request.url); - const cache = await caches.open(CACHES.get("cards_" + langCode)); - cache.put(event.request, response.clone()); - } else if (/sound\/voice\/\w+\/padv\d+.wav/i.test(event.request.url)) { //缓存音效 - let regRes = /\/(ja|en|ko)\//i.exec(event.request.url); - let langCode = regRes[1]; - console.debug("缓存Voice-" + langCode, event.request.url); - const cache = await caches.open(CACHES.get("voice_" + langCode)); - cache.put(event.request, response.clone()); - } - return response; - }); + const url = new URL(event.request.url); + const path = url.pathname; + if (/\.json$/i.test(path)) { //json数据优先通过网络获取 + return fetch(event.request).then(async function(response) { + console.debug("缓存数据", url); + const cache = await caches.open(CACHES.get("data")); + cache.put(url.origin + path, response.clone()); + return response; + }).catch((err)=>resp); + } else { //其他的优先使用缓存 + if (resp) console.debug("找到缓存", event.request.url); + return resp || fetch(event.request).then(async function(response) { + //console.debug("正则测试",/images\/cards\w+\/CARDS_\d+\.PNG/i.test(event.request.url)); + if (/\.(html|js|css|wasm)$/i.test(path) || + /\/images\/[\w\-]+\.(png|svg)/i.test(path)) { //缓存程序 + console.debug("缓存程序", url); + const cache = await caches.open(CACHES.get("program")); + cache.put(event.request, response.clone()); + } else if (/\/doc\//i.test(path)) { //缓存文档 + console.debug("缓存文档", url); + const cache = await caches.open(CACHES.get("document")); + cache.put(event.request, response.clone()); + } else if (/\.woff2$/i.test(path)) { //缓存字体 + console.debug("缓存字体", url); + const cache = await caches.open(CACHES.get("font")); + cache.put(event.request, response.clone()); + } else if (/images\/cards_\w+\/CARDS_\d+\.PNG/i.test(path)) { //缓存卡片图 + let regRes = /cards_(\w+)/i.exec(path); + let langCode = regRes[1]; + console.debug("缓存Cards-" + langCode, url); + const cache = await caches.open(CACHES.get("cards_" + langCode)); + cache.put(event.request, response.clone()); + } else if (/sound\/voice\/\w+\/padv\d+.wav/i.test(path)) { //缓存音效 + let regRes = /\/(\w+)\//i.exec(path); + let langCode = regRes[1]; + console.debug("缓存Voice-" + langCode, url); + const cache = await caches.open(CACHES.get("voice_" + langCode)); + cache.put(event.request, response.clone()); + } + return response; + }); + } }) ); }); \ No newline at end of file