From 9b00e7ce58abe6256d70f8b93d479319169ebb26 Mon Sep 17 00:00:00 2001 From: kicer Date: Sat, 5 Sep 2026 15:56:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor(pwa):=20=E5=9B=9E=E6=92=A4=E8=87=B3=20?= =?UTF-8?q?eb4ae32=20=E7=BD=91=E7=BB=9C=E4=BC=98=E5=85=88=E7=AD=96?= =?UTF-8?q?=E7=95=A5,=E5=9B=BE=E7=89=87/JSON=20=E6=94=B9=E4=B8=BA=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E4=BC=98=E5=85=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - git reset --hard 回撤 3cc9296(纯网络转发版,该提交未推送远程),恢复 eb4ae32 - HTML/CSS/JS 保持网络优先、失败回退缓存,代码改动即时生效无需升版本 - 图片(.png/.jpg/.gif/.webp/.svg/.ico/.avif/.bmp)与 JSON(manifest.json 等) 改为缓存优先:内容稳定,命中即返回省流量、弱网加载快,改动时升 CACHE_VERSION 刷新 - /api/ 与 /sse 仍完全跳过,登录验证与流式接口不受缓存影响 --- app/server/public/sw.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/app/server/public/sw.js b/app/server/public/sw.js index d6b5824..3025409 100644 --- a/app/server/public/sw.js +++ b/app/server/public/sw.js @@ -1,6 +1,7 @@ // Agent 任务台 Service Worker -// 策略:HTML 与静态资源均网络优先(保证改动/登录态即时生效),失败回退缓存(离线兜底)。 -// CACHE_VERSION 仅作缓存命名空间,改动静态资源后无需再手动升级它。 +// 策略:HTML 与代码资源(CSS/JS 等)网络优先(保证改动/登录态即时生效),失败回退缓存(离线兜底); +// 图片与 JSON(manifest/图标,内容稳定)缓存优先,命中本地缓存直接返回,省流量、弱网加载快。 +// CACHE_VERSION 仅作缓存命名空间:改 CSS/JS 无需手动升级;改图片/JSON 才需要升它刷新旧缓存。 const CACHE_VERSION = 'agent-v2'; const STATIC_CACHE = `${CACHE_VERSION}-static`; @@ -72,7 +73,25 @@ self.addEventListener('fetch', (event) => { return; } - // 静态资源:网络优先(改完即生效),失败回退缓存(离线/断网可用) + // 图片与 JSON(manifest/图标等,内容稳定):缓存优先,命中即直接返回省流量; + // 未命中走网络并写入缓存。这类资源改动后需升 CACHE_VERSION 刷新旧缓存。 + if (/\.(png|jpe?g|gif|webp|svg|ico|avif|bmp)$/i.test(url.pathname) || /\.json$/i.test(url.pathname)) { + 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; + }); + }) + ); + return; + } + + // 其余静态资源:网络优先(改完即生效),失败回退缓存(离线/断网可用) event.respondWith( fetch(request).then((resp) => { if (resp && resp.status === 200 && resp.type === 'basic') {