58 lines
1.8 KiB
JavaScript
58 lines
1.8 KiB
JavaScript
const CACHE_NAME = 'township-schools-v2';
|
|
const STATIC_ASSETS = [
|
|
'/',
|
|
'/index.php',
|
|
'/admin.php',
|
|
'/learners.php',
|
|
'/assets/css/custom.css',
|
|
'/assets/js/main.js',
|
|
'https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css',
|
|
'https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css',
|
|
'https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap'
|
|
];
|
|
|
|
// Install Event
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => {
|
|
console.log('Caching static assets');
|
|
return cache.addAll(STATIC_ASSETS);
|
|
})
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
// Activate Event
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then((keys) => {
|
|
return Promise.all(
|
|
keys.filter(key => key !== CACHE_NAME)
|
|
.map(key => caches.delete(key))
|
|
);
|
|
})
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
// Fetch Event (Stale-While-Revalidate)
|
|
self.addEventListener('fetch', (event) => {
|
|
// Only handle GET requests
|
|
if (event.request.method !== 'GET') return;
|
|
|
|
event.respondWith(
|
|
caches.open(CACHE_NAME).then((cache) => {
|
|
return cache.match(event.request).then((cachedResponse) => {
|
|
const fetchedResponse = fetch(event.request).then((networkResponse) => {
|
|
cache.put(event.request, networkResponse.clone());
|
|
return networkResponse;
|
|
}).catch(() => {
|
|
// If network fails and no cache, maybe return a fallback page
|
|
return cachedResponse;
|
|
});
|
|
|
|
return cachedResponse || fetchedResponse;
|
|
});
|
|
})
|
|
);
|
|
}); |