76 lines
3.9 KiB
JavaScript
76 lines
3.9 KiB
JavaScript
/* Service Worker — 版本更新检测(学 opentu 的 PWA 机制)
|
|
* 设计要点:
|
|
* - 只负责"检测新版本 + 保持 waiting 等用户确认",【不拦截/不缓存任何 app 资源】,
|
|
* 因此绝不会缓存旧版 app.js 导致"看不到更新",也没有离线白屏风险。
|
|
* - build 时注入版本号,每次发版 sw.js 内容变化 → 浏览器检测到新 SW → 保持 waiting → 前端提示。
|
|
* - 前端发 {type:'SKIP_WAITING'} 消息 → 本 SW skipWaiting 激活新版本 → controllerchange → 页面 reload。
|
|
*/
|
|
const VERSION = '__IWB_VERSION__';
|
|
|
|
self.addEventListener('install', () => {
|
|
// 有旧版 active=版本更新:保持 waiting,不打断当前用户,等前端确认后 skipWaiting。
|
|
// 首次安装(无 active):直接接管。
|
|
const isUpdate = Boolean(self.registration && self.registration.active);
|
|
if (!isUpdate) { try { self.skipWaiting(); } catch (e) { /* ignore */ } }
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
// 清理历史遗留:2026-11-07 曾短暂启用「应用外壳缓存」(离线可用),同日因升级时序
|
|
// 问题(普通刷新白屏)移除。已装过外壳的用户浏览器里会残留 iwb-shell-* 缓存,
|
|
// 在 activate 时一次性删掉,避免旧 index.html/app.js 被继续命中。
|
|
if (event && typeof event.waitUntil === 'function') {
|
|
event.waitUntil((async () => {
|
|
try {
|
|
const keys = await caches.keys();
|
|
await Promise.all(keys.filter((k) => k.indexOf('iwb-shell-') === 0).map((k) => caches.delete(k)));
|
|
} catch (e) { /* ignore */ }
|
|
})().catch(() => {}));
|
|
}
|
|
try { self.clients.claim(); } catch (e) { /* ignore */ }
|
|
});
|
|
|
|
self.addEventListener('message', (event) => {
|
|
const data = event && event.data;
|
|
if (data && data.type === 'SKIP_WAITING') {
|
|
try { self.skipWaiting(); } catch (e) { /* ignore */ }
|
|
}
|
|
});
|
|
|
|
// 图片缓存兜底:仅缓存「外部图床」的 image 请求(request.destination==='image' 且非同源)。
|
|
// app 资源/同源请求一律不拦截(走浏览器默认),保证每次拿到最新版。
|
|
// SWR:命中缓存返回(远程失效也有图);否则 fetch 后缓存成功/opaque 响应用于显示。
|
|
self.addEventListener('fetch', (event) => {
|
|
const req = event && event.request;
|
|
if (!req || req.method !== 'GET' || req.destination !== 'image') return;
|
|
const url = String(req.url || '');
|
|
// 方案A:本地图片缓存虚拟路径 /__iwb_cache__/<hash>(同源)→ 直接读 Cache Storage 返回,绕过默认缓存
|
|
if (/\/__iwb_cache__\//.test(url) && url.indexOf(self.location.origin) === 0) {
|
|
event.respondWith((async () => {
|
|
const cache = await caches.open('iwb-images-local').catch(() => null);
|
|
if (cache) { const hit = await cache.match(req).catch(() => null); if (hit) return hit; }
|
|
return Response.error();
|
|
})());
|
|
return;
|
|
}
|
|
if (!/^https?:/i.test(url)) return;
|
|
if (url.indexOf(self.location.origin) === 0) return; // 同源 app 资源不确定缓存
|
|
event.respondWith((async () => {
|
|
const cache = await caches.open('iwb-images').catch(() => null);
|
|
if (cache) { const hit = await cache.match(req).catch(() => null); if (hit) return hit; }
|
|
try {
|
|
const net = await fetch(req);
|
|
// 只缓存「可显示」的响应(ok 或跨域 opaque),不缓存 403/4xx
|
|
if (cache && net && (net.ok || net.type === 'opaque')) { try { await cache.put(req, net.clone()); } catch (e) { /* ignore */ } }
|
|
// 容量/TTL 管理:图片缓存条目过多时清最旧,避免无限增长
|
|
try {
|
|
const keys = await cache.keys();
|
|
if (keys.length > 300) { for (let i = 0; i < keys.length - 300; i++) { try { await cache.delete(keys[i]); } catch (e) { /* ignore */ } } }
|
|
} catch (e) { /* ignore */ }
|
|
return net;
|
|
} catch (e) {
|
|
if (cache) { const c = await cache.match(req).catch(() => null); if (c) return c; }
|
|
return Response.error();
|
|
}
|
|
})());
|
|
});
|