Simplify Service Worker

This commit is contained in:
Leo Herzog
2025-12-22 22:06:43 -05:00
parent 570df30bcc
commit 74d74f8432

View File

@@ -1,84 +1,38 @@
const cache_name = 'torrent-parts-v1';
const assets = ['/', '/index.html', '/src/parse.js', '/src/style.css', '/ext/alata-v9-latin-regular.woff2', '/ext/alata-v9-latin-regular.ttf', '/ext/fa.min.js', '/ext/jj2008-06-14.mk4_archive.torrent', '/favicon.ico', '/manifest.webmanifest'];
const CACHE_NAME = 'torrent-parts-v2';
const ASSETS = [
'/',
'/index.html',
'/src/parse.js',
'/src/style.css',
'/ext/alata-v9-latin-regular.woff2',
'/ext/fa.min.js',
'/ext/jj2008-06-14.mk4_archive.torrent',
'/favicon.ico',
'/manifest.webmanifest',
];
self.addEventListener('install', function (event) {
self.skipWaiting(); // Force activate new SW immediately
event.waitUntil(
caches
.open(cache_name)
.then(function (cache) {
return cache.addAll(assets);
})
.catch(function (error) {
console.error('Service worker install failed:', error);
})
);
self.addEventListener('install', (e) => {
self.skipWaiting();
e.waitUntil(caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS)));
});
self.addEventListener('activate', function (event) {
event.waitUntil(
self.addEventListener('activate', (e) => {
e.waitUntil(
Promise.all([
// Take control of all clients immediately
self.clients.claim(),
// Clean up old caches
caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.map(function (cacheName) {
if (cacheName !== cache_name) {
console.log('Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
}),
caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k)))),
])
);
});
self.addEventListener('fetch', function (event) {
// Only cache GET requests
if (event.request.method !== 'GET') {
return;
}
self.addEventListener('fetch', (e) => {
if (e.request.method !== 'GET') return;
// Network-first strategy for external requests
const requestUrl = new URL(event.request.url);
if (requestUrl.origin !== self.location.origin) {
event.respondWith(
fetch(event.request)
.then(function (response) {
return response;
})
.catch(function () {
console.log('Network request failed, trying cache:', event.request.url);
return caches.match(event.request);
})
);
return;
}
const url = new URL(e.request.url);
// Cache-first strategy for app assets
event.respondWith(
caches
.match(event.request)
.then(function (response) {
if (response) {
return response;
}
return fetch(event.request).then(function (response) {
// Cache successful responses for future use
if (response && response.status === 200) {
const responseClone = response.clone();
caches.open(cache_name).then(function (cache) {
cache.put(event.request, responseClone);
});
}
return response;
});
})
.catch(function (error) {
console.error('Service worker fetch failed:', error);
// Could return offline fallback page here if needed
})
);
// Only cache same-origin requests
if (url.origin !== self.location.origin) return;
// Cache-first for local assets
e.respondWith(caches.match(e.request).then((cached) => cached || fetch(e.request)));
});