37970-vm/sw.js
Flatlogic Bot 57a1969d5e v4
2026-01-30 14:56:15 +00:00

69 lines
2.1 KiB
JavaScript

const CACHE_NAME = 'township-schools-v5';
const STATIC_ASSETS = [
'/',
'/index.php',
'/login.php',
'/register.php',
'/teacher.php',
'/admin.php',
'/learners.php',
'/assessments.php',
'/reports.php',
'/bulk-upload.php',
'/notifications.php',
'/parent.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).catch(err => console.warn('PWA Cache error:', err));
})
);
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 and local/trusted domains
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) => {
// Only cache successful GET responses
if (networkResponse.ok) {
cache.put(event.request, networkResponse.clone());
}
return networkResponse;
}).catch(() => {
return cachedResponse;
});
return cachedResponse || fetchedResponse;
});
})
);
});