feat: 综合平台services多服务支持+SW网络优先缓存修复+适配层/定时任务完善
This commit is contained in:
+23
-6
@@ -1,12 +1,13 @@
|
||||
/* vps-manager Service Worker
|
||||
* 缓存策略:
|
||||
* - 预缓存核心静态资源,安装即可离线打开
|
||||
* - 静态资源与页面:stale-while-revalidate(先返回缓存秒开,后台自动拉取最新版本)
|
||||
* - 页面导航(HTML):network-first,回源失败才用缓存。保证刷新即拿到最新版本号
|
||||
* - 静态资源(?v= 版本号):stale-while-revalidate(先返回缓存秒开,后台自动拉取最新)
|
||||
* —— 版本号变了 URL 就变,缓存必 miss,天然回源新文件
|
||||
* - /api/* 数据请求:始终走网络,保证数据实时
|
||||
* 更新机制:业务代码更新后,后台自动同步到缓存,下次访问生效;
|
||||
* 若需强制刷新缓存,递增 CACHE_NAME 即可清理旧缓存。
|
||||
* 更新机制:sw.js 由服务端 no-cache 提供,改动后导航时立即被浏览器发现;
|
||||
* install 后 skipWaiting + clients.claim 立即接管,无需手动清缓存。
|
||||
*/
|
||||
const CACHE_NAME = 'vps-manager-v1';
|
||||
const CACHE_NAME = 'vps-manager-v3';
|
||||
|
||||
const PRECACHE_URLS = [
|
||||
'/',
|
||||
@@ -48,10 +49,26 @@ self.addEventListener('fetch', (event) => {
|
||||
// 数据接口走网络,不缓存
|
||||
if (url.pathname.startsWith('/api/')) return;
|
||||
|
||||
// 页面导航:network-first,保证每次刷新都拿到最新 HTML(含最新版本号)
|
||||
if (request.mode === 'navigate') {
|
||||
event.respondWith(
|
||||
fetch(request, { cache: 'no-store' })
|
||||
.then((response) => {
|
||||
const copy = response.clone();
|
||||
caches.open(CACHE_NAME).then((cache) => cache.put(request, copy));
|
||||
return response;
|
||||
})
|
||||
.catch(() => caches.match(request))
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// 静态资源:stale-while-revalidate(版本号控制缓存失效)
|
||||
event.respondWith(
|
||||
caches.open(CACHE_NAME).then(async (cache) => {
|
||||
const cached = await cache.match(request);
|
||||
const network = fetch(request)
|
||||
// no-store:绕开浏览器 HTTP 磁盘缓存,保证回源拿到最新版本
|
||||
const network = fetch(request, { cache: 'no-store' })
|
||||
.then((response) => {
|
||||
if (response && response.ok) cache.put(request, response.clone());
|
||||
return response;
|
||||
|
||||
Reference in New Issue
Block a user