52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
// Updated service worker code - sw.js
|
|
const CACHE_NAME = 'timer-cache-v1';
|
|
const urlsToCache = [
|
|
'/',
|
|
'/index.html',
|
|
'/styles.css',
|
|
'/apps.js',
|
|
'/audio.js',
|
|
'/icons/android-chrome-192x192.png',
|
|
'/icons/android-chrome-512x512.png',
|
|
'/icons/apple-touch-icon.png',
|
|
'/icons/favicon-32x32.png',
|
|
'/icons/favicon-16x16.png',
|
|
'/favicon.ico',
|
|
'/manifest.json',
|
|
'/site.webmanifest'
|
|
];
|
|
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => {
|
|
console.log('Opened cache');
|
|
// Use individual cache.add calls in a Promise.all to handle failures better
|
|
return Promise.all(
|
|
urlsToCache.map(url => {
|
|
return cache.add(url).catch(err => {
|
|
console.log('Failed to cache:', url, err);
|
|
// Continue despite individual failures
|
|
return Promise.resolve();
|
|
});
|
|
})
|
|
);
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', event => {
|
|
event.respondWith(
|
|
caches.match(event.request)
|
|
.then(response => {
|
|
// Cache hit - return response
|
|
if (response) {
|
|
return response;
|
|
}
|
|
return fetch(event.request);
|
|
})
|
|
.catch(err => {
|
|
console.log('Fetch handler failed:', err);
|
|
})
|
|
);
|
|
}); |