111 lines
3.2 KiB
JavaScript
111 lines
3.2 KiB
JavaScript
const CACHE_VERSION = '2026-07-17-v1';
|
|
const CACHE_PREFIX = 'bis-blog-';
|
|
const CORE_CACHE = `${CACHE_PREFIX}core-${CACHE_VERSION}`;
|
|
const PAGE_CACHE = `${CACHE_PREFIX}pages-${CACHE_VERSION}`;
|
|
const ASSET_CACHE = `${CACHE_PREFIX}assets-${CACHE_VERSION}`;
|
|
const IMAGE_CACHE = `${CACHE_PREFIX}images-${CACHE_VERSION}`;
|
|
|
|
const CORE_ASSETS = [
|
|
'/',
|
|
'/offline.html',
|
|
'/manifest.webmanifest',
|
|
'/images/Bi.ico',
|
|
'/images/pwa-192x192.png',
|
|
'/images/pwa-512x512.png',
|
|
'/images/pwa-maskable-512x512.png',
|
|
'/images/apple-touch-icon.png',
|
|
];
|
|
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
caches.open(CORE_CACHE)
|
|
.then((cache) => cache.addAll(CORE_ASSETS))
|
|
);
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil((async () => {
|
|
const cacheNames = await caches.keys();
|
|
await Promise.all(
|
|
cacheNames
|
|
.filter((name) => name.startsWith(CACHE_PREFIX) && ![CORE_CACHE, PAGE_CACHE, ASSET_CACHE, IMAGE_CACHE].includes(name))
|
|
.map((name) => caches.delete(name))
|
|
);
|
|
await self.clients.claim();
|
|
const clients = await self.clients.matchAll({ type: 'window' });
|
|
clients.forEach((client) => client.postMessage({ type: 'OFFLINE_READY' }));
|
|
})());
|
|
});
|
|
|
|
self.addEventListener('message', (event) => {
|
|
if (event.data?.type === 'SKIP_WAITING') self.skipWaiting();
|
|
});
|
|
|
|
async function trimCache(cacheName, maxEntries) {
|
|
const cache = await caches.open(cacheName);
|
|
const keys = await cache.keys();
|
|
if (keys.length > maxEntries) {
|
|
await Promise.all(keys.slice(0, keys.length - maxEntries).map((key) => cache.delete(key)));
|
|
}
|
|
}
|
|
|
|
async function networkFirst(request) {
|
|
try {
|
|
const response = await fetch(request);
|
|
if (response.ok) {
|
|
const cache = await caches.open(PAGE_CACHE);
|
|
await cache.put(request, response.clone());
|
|
void trimCache(PAGE_CACHE, 40);
|
|
}
|
|
return response;
|
|
} catch {
|
|
return (await caches.match(request)) || (await caches.match('/offline.html'));
|
|
}
|
|
}
|
|
|
|
async function cacheFirst(request, cacheName, maxEntries) {
|
|
const cached = await caches.match(request);
|
|
if (cached) return cached;
|
|
|
|
const response = await fetch(request);
|
|
if (response.ok || response.type === 'opaque') {
|
|
const cache = await caches.open(cacheName);
|
|
await cache.put(request, response.clone());
|
|
void trimCache(cacheName, maxEntries);
|
|
}
|
|
return response;
|
|
}
|
|
|
|
async function staleWhileRevalidate(request) {
|
|
const cache = await caches.open(ASSET_CACHE);
|
|
const cached = await cache.match(request);
|
|
const network = fetch(request).then((response) => {
|
|
if (response.ok) cache.put(request, response.clone());
|
|
return response;
|
|
}).catch(() => cached);
|
|
return cached || network;
|
|
}
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
const { request } = event;
|
|
if (request.method !== 'GET') return;
|
|
|
|
const url = new URL(request.url);
|
|
if (request.mode === 'navigate') {
|
|
event.respondWith(networkFirst(request));
|
|
return;
|
|
}
|
|
|
|
if (request.destination === 'image') {
|
|
event.respondWith(cacheFirst(request, IMAGE_CACHE, 100));
|
|
return;
|
|
}
|
|
|
|
if (
|
|
url.origin === self.location.origin &&
|
|
['style', 'script', 'font', 'worker'].includes(request.destination)
|
|
) {
|
|
event.respondWith(staleWhileRevalidate(request));
|
|
}
|
|
});
|