61 lines
1.8 KiB
JavaScript

const CACHE_NAME = 'salvagedb-v1';
const urlsToCache = [
'/',
'/static/styles.css',
'/static/slogo.png',
'/static/manifest.json',
'/static/icons/icon-72x72.png',
'/static/icons/icon-96x96.png',
'/static/icons/icon-128x128.png',
'/static/icons/icon-144x144.png',
'/static/icons/icon-152x152.png',
'/static/icons/icon-192x192.png',
'/static/icons/icon-384x384.png',
'/static/icons/icon-512x512.png',
'https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css',
'https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js',
'https://www.google.com/recaptcha/api.js'
];
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
.catch(error => {
console.error('Cache failed:', error);
})
);
});
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(response => {
if (response) {
return response;
}
return fetch(event.request)
.then(response => {
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
const responseToCache = response.clone();
caches.open(CACHE_NAME)
.then(cache => {
cache.put(event.request, responseToCache);
});
return response;
})
.catch(error => {
console.error('Fetch failed:', error);
return new Response('Network error happened', {
status: 408,
headers: { 'Content-Type': 'text/plain' },
});
});
})
);
});