50 lines
1.0 KiB
JavaScript
50 lines
1.0 KiB
JavaScript
const CACHE_NAME = 'financial-offline-v20260721-nocache1';
|
|
const STATIC_ASSETS = [
|
|
'/offline.html'
|
|
];
|
|
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => cache.addAll(STATIC_ASSETS))
|
|
.catch(() => undefined)
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', event => {
|
|
event.waitUntil(
|
|
caches.keys().then(keys => Promise.all(
|
|
keys.filter(key => key !== CACHE_NAME).map(key => caches.delete(key))
|
|
))
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener('fetch', event => {
|
|
const request = event.request;
|
|
|
|
if (request.method !== 'GET') {
|
|
return;
|
|
}
|
|
|
|
const url = new URL(request.url);
|
|
|
|
if (url.origin !== location.origin) {
|
|
return;
|
|
}
|
|
|
|
if (url.pathname.startsWith('/api/')) {
|
|
return;
|
|
}
|
|
|
|
if (request.mode === 'navigate') {
|
|
event.respondWith(
|
|
fetch(request, { cache: 'no-store' }).catch(() => caches.match('/offline.html'))
|
|
);
|
|
return;
|
|
}
|
|
|
|
event.respondWith(fetch(request, { cache: 'no-store' }));
|
|
});
|