debrid-services-comparison/dist/sw.js
2025-01-25 20:03:04 +05:00

69 lines
No EOL
1.6 KiB
JavaScript

const CACHE_VERSION = 'v1';
const CACHE_NAME = `debrid-cache-${CACHE_VERSION}`;
const STATIC_ASSETS = [
'/',
'/index.html',
'/css/styles-min.css',
'/js/app-min.js',
'/favicon.ico',
'/favicon.svg',
'/apple-touch-icon.png'
];
const DATA_ASSETS = [
'/json/pricing.json',
'/json/file-hosts.json',
'/json/adult-hosts.json'
];
// Install event - cache static assets
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME).then(cache => {
return cache.addAll([...STATIC_ASSETS, ...DATA_ASSETS]);
})
);
});
// Activate event - clean old caches
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames
.filter(name => name !== CACHE_NAME)
.map(name => caches.delete(name))
);
})
);
});
// Fetch event - serve from cache, fallback to network
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request).then(response => {
// Return cached response if found
if (response) {
return response;
}
// Clone the request - stream can only be consumed once
const fetchRequest = event.request.clone();
// Make network request and cache the response
return fetch(fetchRequest).then(response => {
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
const responseToCache = response.clone();
caches.open(CACHE_NAME).then(cache => {
cache.put(event.request, responseToCache);
});
return response;
});
})
);
});