From eb4ae32d4ac62f39fc294b27dfc5abeca6889e3e Mon Sep 17 00:00:00 2001 From: kicer Date: Sat, 5 Sep 2026 15:44:18 +0800 Subject: [PATCH] =?UTF-8?q?refactor(pwa):=20=E9=9D=99=E6=80=81=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E6=94=B9=E4=B8=BA=E7=BD=91=E7=BB=9C=E4=BC=98=E5=85=88?= =?UTF-8?q?,=E5=85=8D=E9=99=A4=E6=AF=8F=E6=AC=A1=E6=89=8B=E5=8A=A8?= =?UTF-8?q?=E5=8D=87=20CACHE=5FVERSION?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原缓存优先策略下,每次改 CSS/JS 都要手动改 CACHE_VERSION 才能清旧缓存, 漏改就会像之前那样移动端拿到旧样式。改为网络优先:在线时始终取最新, 离线/失败时回退预缓存,与 HTML 文档的处理一致,CACHE_VERSION 仅作命名空间。 --- app/server/public/sw.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/app/server/public/sw.js b/app/server/public/sw.js index 07126f1..d6b5824 100644 --- a/app/server/public/sw.js +++ b/app/server/public/sw.js @@ -1,5 +1,6 @@ // Agent 任务台 Service Worker -// 策略:HTML 网络优先(保证登录态/最新版本),静态资源缓存优先(图标/CSS/JS/vendor) +// 策略:HTML 与静态资源均网络优先(保证改动/登录态即时生效),失败回退缓存(离线兜底)。 +// CACHE_VERSION 仅作缓存命名空间,改动静态资源后无需再手动升级它。 const CACHE_VERSION = 'agent-v2'; const STATIC_CACHE = `${CACHE_VERSION}-static`; @@ -71,18 +72,15 @@ self.addEventListener('fetch', (event) => { return; } - // 静态资源:缓存优先 + // 静态资源:网络优先(改完即生效),失败回退缓存(离线/断网可用) event.respondWith( - caches.match(request).then((cached) => { - if (cached) return cached; - return fetch(request).then((resp) => { - if (resp && resp.status === 200 && resp.type === 'basic') { - const copy = resp.clone(); - caches.open(RUNTIME_CACHE).then((cache) => cache.put(request, copy)); - } - return resp; - }).catch(() => cached); - }) + fetch(request).then((resp) => { + if (resp && resp.status === 200 && resp.type === 'basic') { + const copy = resp.clone(); + caches.open(RUNTIME_CACHE).then((cache) => cache.put(request, copy)); + } + return resp; + }).catch(() => caches.match(request)) ); });