82 lines
2.4 KiB
JavaScript

const CACHE_NAME = 'salvagedb-v1';
const urlsToCache = [
'/',
'/static/styles.css',
'/static/styles__ltr.css',
'/static/privacy.css',
'/static/slogo.png',
'/static/logo2.png',
'/static/salvagedblogo.png',
'/static/favicon.ico',
'/static/faviconV2.png',
'/static/curved-arrow.png',
'/static/vin-position1.gif',
'/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 Promise.all(
urlsToCache.map(url =>
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(`Failed to fetch ${url}`);
}
return cache.put(url, response);
})
.catch(error => {
console.warn(`Failed to cache ${url}:`, error);
})
)
);
})
.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' },
});
});
})
);
});