Files
agent/app/server/public/sw.js
T
kicer 6c71316f13 fix(pwa): 升 Service Worker 缓存版本,移动端任务文件目录图标显示为黑块
根因:sw.js 对 /css/style.css 走缓存优先且 CACHE_VERSION 一直是 agent-v1,
上一轮给 style.css 新增的 .file-dir-* 目录图标样式没送达移动端,
目录头里的 <svg> 缺尺寸/fill:none 样式,按 SVG 默认规则渲染成又大又黑的一团。
桌面端没被旧缓存拦截所以正常。
升到 agent-v2:install 时重新预缓存新资源,activate 时清理 agent-v1-* 旧缓存。
2026-09-05 15:41:59 +08:00

91 lines
2.5 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/vendor)
const CACHE_VERSION = 'agent-v2';
const STATIC_CACHE = `${CACHE_VERSION}-static`;
const RUNTIME_CACHE = `${CACHE_VERSION}-runtime`;
const PRECACHE_URLS = [
'/',
'/login.html',
'/css/style.css',
'/js/core.js',
'/js/models.js',
'/js/settings.js',
'/js/skills.js',
'/js/tasks.js',
'/vendor/marked.min.js',
'/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;
}
// 静态资源:缓存优先
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);
})
);
});
self.addEventListener('message', (event) => {
if (event.data === 'SKIP_WAITING') self.skipWaiting();
});