47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
const CACHE_NAME = 'donation-platform-v2'; // Changed cache name
|
|
const urlsToCache = [
|
|
'assets/css/custom.css',
|
|
'assets/js/main.js'
|
|
];
|
|
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => {
|
|
return cache.addAll(urlsToCache);
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('activate', event => {
|
|
var cacheKeeplist = [CACHE_NAME];
|
|
event.waitUntil(
|
|
caches.keys().then(keylist => {
|
|
return Promise.all(keylist.map(key => {
|
|
if (cacheKeeplist.indexOf(key) === -1) {
|
|
return caches.delete(key);
|
|
}
|
|
}));
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', event => {
|
|
// Use a network-first strategy for HTML pages
|
|
if (event.request.mode === 'navigate' || (event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html'))) {
|
|
event.respondWith(
|
|
fetch(event.request).catch(() => {
|
|
return caches.match(event.request);
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Use a cache-first strategy for other assets
|
|
event.respondWith(
|
|
caches.match(event.request)
|
|
.then(response => {
|
|
return response || fetch(event.request);
|
|
})
|
|
);
|
|
}); |