mirror of
https://github.com/leoherzog/TorrentParts.git
synced 2026-01-23 19:58:03 -08:00
Simplify Service Worker
This commit is contained in:
98
src/sw.js
98
src/sw.js
@@ -1,84 +1,38 @@
|
|||||||
const cache_name = 'torrent-parts-v1';
|
const CACHE_NAME = 'torrent-parts-v2';
|
||||||
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 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.addEventListener('install', (e) => {
|
||||||
self.skipWaiting(); // Force activate new SW immediately
|
self.skipWaiting();
|
||||||
event.waitUntil(
|
e.waitUntil(caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS)));
|
||||||
caches
|
|
||||||
.open(cache_name)
|
|
||||||
.then(function (cache) {
|
|
||||||
return cache.addAll(assets);
|
|
||||||
})
|
|
||||||
.catch(function (error) {
|
|
||||||
console.error('Service worker install failed:', error);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('activate', function (event) {
|
self.addEventListener('activate', (e) => {
|
||||||
event.waitUntil(
|
e.waitUntil(
|
||||||
Promise.all([
|
Promise.all([
|
||||||
// Take control of all clients immediately
|
|
||||||
self.clients.claim(),
|
self.clients.claim(),
|
||||||
// Clean up old caches
|
caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k)))),
|
||||||
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);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('fetch', function (event) {
|
self.addEventListener('fetch', (e) => {
|
||||||
// Only cache GET requests
|
if (e.request.method !== 'GET') return;
|
||||||
if (event.request.method !== 'GET') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Network-first strategy for external requests
|
const url = new URL(e.request.url);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cache-first strategy for app assets
|
// Only cache same-origin requests
|
||||||
event.respondWith(
|
if (url.origin !== self.location.origin) return;
|
||||||
caches
|
|
||||||
.match(event.request)
|
// Cache-first for local assets
|
||||||
.then(function (response) {
|
e.respondWith(caches.match(e.request).then((cached) => cached || fetch(e.request)));
|
||||||
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
|
|
||||||
})
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user