Files
webnews/static/sw.js
Sudo-Ivan 3cdca944ef
Some checks failed
renovate / renovate (push) Failing after 4s
OSV-Scanner Scheduled Scan / scan-scheduled (push) Successful in 54s
CI / build-frontend (push) Successful in 1m6s
CI / build-backend (push) Successful in 36s
Build and Publish Docker Image / build (push) Successful in 11m1s
Publish / publish (push) Successful in 33m35s
0.2.3
2025-12-27 21:27:15 -06:00

75 lines
2.0 KiB
JavaScript

const CACHE_VERSION = '0.2.3';
const CACHE_NAME = `web-news-${CACHE_VERSION}`;
const urlsToCache = ['/', '/favicon.svg', '/manifest.json'];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(urlsToCache).catch(() => {
console.log('Some resources failed to cache');
});
})
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME && cacheName.startsWith('web-news-')) {
return caches.delete(cacheName);
}
})
);
})
);
self.clients.claim();
});
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});
self.addEventListener('fetch', (event) => {
if (event.request.method !== 'GET') return;
const url = new URL(event.request.url);
// Don't cache API calls or external RSS fetches
if (url.pathname.startsWith('/api') || !url.origin.startsWith(self.location.origin)) {
return;
}
// Network-First Strategy for everything else
// This ensures you always see the latest version if online,
// and only see the cached version if truly offline.
event.respondWith(
fetch(event.request)
.then((response) => {
// If valid response, clone and cache it
if (response && response.status === 200 && response.type === 'basic') {
const responseToCache = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseToCache);
});
}
return response;
})
.catch(() => {
// If network fails, try the cache
return caches.match(event.request).then((response) => {
if (response) return response;
// Fallback to index.html for SPA routing offline
if (event.request.mode === 'navigate') {
return caches.match('/');
}
return new Response('Offline', { status: 503 });
});
})
);
});