Files
isal.in/public/sw.js
T

103 lines
3.1 KiB
JavaScript

const CACHE_NAME = 'mc-cache-v6';
const PRECACHE_ASSETS = [
'/',
'/index.html',
'/manifest.json',
'/favicon.svg',
];
// Domains that should NEVER be cached or block offline usage (e.g. ad networks)
const IGNORE_HOSTS = [
'media.ethicalads.io',
'server.ethicalads.io',
'pagead2.googlesyndication.com',
'googleads.g.doubleclick.net',
];
// Install: Pre-cache shell assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(PRECACHE_ASSETS);
}).then(() => self.skipWaiting())
);
});
// Activate: Clean up old versions
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) => {
return Promise.all(
keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key))
);
}).then(() => self.clients.claim())
);
});
// Fetch: Caching strategies
self.addEventListener('fetch', (event) => {
const { request } = event;
const url = new URL(request.url);
// Ignore non-http(s) requests or ad scripts
if (!url.protocol.startsWith('http')) return;
if (IGNORE_HOSTS.some((host) => url.hostname.includes(host))) {
return; // Let standard browser network handle without intercepting
}
// 1. Navigation requests (HTML document): Network-first with offline fallback
if (request.mode === 'navigate') {
event.respondWith(
fetch(request)
.then((networkResponse) => {
if (networkResponse && networkResponse.status === 200) {
const responseClone = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, responseClone));
}
return networkResponse;
})
.catch(() => {
return caches.match('/index.html').then((cached) => cached || caches.match('/'));
})
);
return;
}
// 2. Static Assets (JS, CSS, SVGs, Fonts): Stale-While-Revalidate
if (
url.origin === self.location.origin &&
(url.pathname.startsWith('/assets/') || url.pathname.endsWith('.svg') || url.pathname.endsWith('.json'))
) {
event.respondWith(
caches.match(request).then((cachedResponse) => {
const fetchPromise = fetch(request)
.then((networkResponse) => {
if (networkResponse && networkResponse.status === 200) {
const responseClone = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, responseClone));
}
return networkResponse;
})
.catch(() => null);
// Return cached asset immediately if available, otherwise wait for network
return cachedResponse || fetchPromise;
})
);
return;
}
// Default: Network with Cache Fallback
event.respondWith(
fetch(request)
.then((networkResponse) => {
if (networkResponse && networkResponse.status === 200 && request.method === 'GET') {
const responseClone = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, responseClone));
}
return networkResponse;
})
.catch(() => caches.match(request))
);
});