refactor(pwa): 静态资源改为网络优先,免除每次手动升 CACHE_VERSION
原缓存优先策略下,每次改 CSS/JS 都要手动改 CACHE_VERSION 才能清旧缓存, 漏改就会像之前那样移动端拿到旧样式。改为网络优先:在线时始终取最新, 离线/失败时回退预缓存,与 HTML 文档的处理一致,CACHE_VERSION 仅作命名空间。
This commit is contained in:
+10
-12
@@ -1,5 +1,6 @@
|
|||||||
// Agent 任务台 Service Worker
|
// Agent 任务台 Service Worker
|
||||||
// 策略:HTML 网络优先(保证登录态/最新版本),静态资源缓存优先(图标/CSS/JS/vendor)
|
// 策略:HTML 与静态资源均网络优先(保证改动/登录态即时生效),失败回退缓存(离线兜底)。
|
||||||
|
// CACHE_VERSION 仅作缓存命名空间,改动静态资源后无需再手动升级它。
|
||||||
|
|
||||||
const CACHE_VERSION = 'agent-v2';
|
const CACHE_VERSION = 'agent-v2';
|
||||||
const STATIC_CACHE = `${CACHE_VERSION}-static`;
|
const STATIC_CACHE = `${CACHE_VERSION}-static`;
|
||||||
@@ -71,18 +72,15 @@ self.addEventListener('fetch', (event) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 静态资源:缓存优先
|
// 静态资源:网络优先(改完即生效),失败回退缓存(离线/断网可用)
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
caches.match(request).then((cached) => {
|
fetch(request).then((resp) => {
|
||||||
if (cached) return cached;
|
if (resp && resp.status === 200 && resp.type === 'basic') {
|
||||||
return fetch(request).then((resp) => {
|
const copy = resp.clone();
|
||||||
if (resp && resp.status === 200 && resp.type === 'basic') {
|
caches.open(RUNTIME_CACHE).then((cache) => cache.put(request, copy));
|
||||||
const copy = resp.clone();
|
}
|
||||||
caches.open(RUNTIME_CACHE).then((cache) => cache.put(request, copy));
|
return resp;
|
||||||
}
|
}).catch(() => caches.match(request))
|
||||||
return resp;
|
|
||||||
}).catch(() => cached);
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user