Files
agent/app/server/public/sw.js
T
kicer 763fc38882 refactor(pwa): 精简 PRECACHE_URLS,仅预缓存缓存优先的图标与 manifest
- 移除 / 和 /login.html: 网络优先 HTML 不预缓存离线更稳(避免 STATIC_CACHE 旧版遮蔽 RUNTIME_CACHE 新版本)
- 移除 /css/style.css: 同上,CSS 在线时每次都写 RUNTIME_CACHE; 离线回退拿最新的
- 移除 /js/core.js 等 5 个 js 文件: 仅 v1.html(遗留版)引用,index.html 全内联无依赖
- 移除 /vendor/marked.min.js: 没有任何页面引用(CDN 加载),是死资源
- 保留 /manifest.json + 3 个 icon .png: 缓存优先的静态资源,安装时预置秒开
2026-09-05 16:00:04 +08:00

102 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Agent 任务台 Service Worker
// 策略:HTML 与代码资源(CSS/JS 等)网络优先(保证改动/登录态即时生效),失败回退缓存(离线兜底);
// 图片与 JSON(manifest/图标,内容稳定)缓存优先,命中本地缓存直接返回,省流量、弱网加载快。
// CACHE_VERSION 仅作缓存命名空间:改 CSS/JS 无需手动升级;改图片/JSON 才需要升它刷新旧缓存。
const CACHE_VERSION = 'agent-v2';
const STATIC_CACHE = `${CACHE_VERSION}-static`;
const RUNTIME_CACHE = `${CACHE_VERSION}-runtime`;
// PRECACHE_URLS: 仅预缓存图片与 JSON——这些是缓存优先的资源,安装时直接落盘秒开,离线也能用。
// HTML/CSS/JS 不预缓存:网络优先策略每次在线都会写入 RUNTIME_CACHE,
// STATIC_CACHE 的旧版反而会在 caches.match() 离线回退时遮蔽更新。
const PRECACHE_URLS = [
'/manifest.json',
'/icons/icon-192.png',
'/icons/icon-512.png',
'/icons/apple-touch-icon.png'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(STATIC_CACHE).then((cache) =>
// addAll 任一失败会整体失败,逐个 add 更稳
Promise.all(
PRECACHE_URLS.map((url) =>
cache.add(url).catch((err) => console.warn('[SW] precache fail', url, err))
)
)
)
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(
keys.filter((k) => !k.startsWith(CACHE_VERSION)).map((k) => caches.delete(k))
)
)
);
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
const { request } = event;
if (request.method !== 'GET') return;
const url = new URL(request.url);
// 只处理同源
if (url.origin !== self.location.origin) return;
// 跳过 API/SSE,避免缓存动态数据
if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/sse')) return;
// HTML/登录页:网络优先
if (request.mode === 'navigate' || request.destination === 'document') {
event.respondWith(
fetch(request)
.then((resp) => {
const copy = resp.clone();
caches.open(RUNTIME_CACHE).then((cache) => cache.put(request, copy));
return resp;
})
.catch(() => caches.match(request).then((r) => r || caches.match('/')))
);
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') {
const copy = resp.clone();
caches.open(RUNTIME_CACHE).then((cache) => cache.put(request, copy));
}
return resp;
}).catch(() => caches.match(request))
);
});
self.addEventListener('message', (event) => {
if (event.data === 'SKIP_WAITING') self.skipWaiting();
});