radical/service-worker.js
Omar Najjar 2e5c99636a pwa
2025-03-15 22:35:29 +11:00

72 lines
No EOL
1.9 KiB
JavaScript

// Service Worker for RADICAL PWA
const CACHE_NAME = 'radical-cache-v1';
const urlsToCache = [
'/',
'/styles.css',
'https://radical.omar-c29.workers.dev/memes/favicon.jpg',
'https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@300;400;500;600;700&display=swap'
];
// Install event - cache essential resources
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('Cache opened');
return cache.addAll(urlsToCache);
})
);
});
// Fetch event - serve from cache when possible
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(response => {
// Return cached response if found
if (response) {
return response;
}
// Clone the request - request can only be used once
const fetchRequest = event.request.clone();
return fetch(fetchRequest).then(response => {
// Check if valid response
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
// Clone the response - response can only be used once
const responseToCache = response.clone();
caches.open(CACHE_NAME).then(cache => {
// Don't cache API responses
if (!event.request.url.includes('/api/')) {
cache.put(event.request, responseToCache);
}
});
return response;
});
})
);
});
// Activate event - clean up old caches
self.addEventListener('activate', event => {
const cacheWhitelist = [CACHE_NAME];
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
);
});