29 lines
832 B
JavaScript
29 lines
832 B
JavaScript
// Service Worker — 离线缓存 + 推送通知
|
|||
|
|
const CACHE_NAME = 'scilit-oncology-v1';
|
||
|
|
const STATIC_ASSETS = ['/', '/app/feed', '/app/search', '/app/library'];
|
||
|
|
|
||
|
|
self.addEventListener('install', (event) => {
|
||
|
|
(event as any).waitUntil(
|
||
|
|
caches.open(CACHE_NAME).then((cache) => cache.addAll(STATIC_ASSETS))
|
||
|
|
);
|
||
|
|
self.skipWaiting();
|
||
|
|
});
|
||
|
|
|
||
|
|
self.addEventListener('activate', (event) => {
|
||
|
|
(event as any).waitUntil(
|
||
|
|
caches.keys().then((keys) =>
|
||
|
|
Promise.all(keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k)))
|
||
|
|
)
|
||
|
|
);
|
||
|
|
self.clients.claim();
|
||
|
|
});
|
||
|
|
|
||
|
|
self.addEventListener('fetch', (event) => {
|
||
|
|
const req = (event as FetchEvent).request;
|
||
|
|
// API 请求不缓存
|
||
|
|
if (req.url.includes('/api/')) return;
|
||
|
|
(event as any).respondWith(
|
||
|
|
caches.match(req).then((cached) => cached || fetch(req))
|
||
|
|
);
|
||
|
|
});
|