54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
// public/sw.js - CORRECTED
|
|
|
|
const CACHE_NAME = 'timepulse-v1';
|
|
const filesToCache = [
|
|
'/', '/index.html', '/style/style.css', '/js/main.js', '/js/ui.js',
|
|
'/js/api.js', '/js/utils.js', '/manifest.json',
|
|
'/icons/icon-192x192.webp', '/icons/icon-512x512.webp'
|
|
];
|
|
|
|
// --- Install Event ---
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(caches.open(CACHE_NAME).then(cache => cache.addAll(filesToCache)));
|
|
self.skipWaiting();
|
|
});
|
|
|
|
// --- Activate Event ---
|
|
self.addEventListener('activate', event => {
|
|
event.waitUntil(
|
|
caches.keys().then(keys => Promise.all(
|
|
keys.map(key => {
|
|
if (key !== CACHE_NAME) {
|
|
return caches.delete(key);
|
|
}
|
|
})
|
|
))
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
// --- Fetch Event ---
|
|
self.addEventListener('fetch', event => {
|
|
if (event.request.method !== 'GET' || event.request.url.includes('/api/')) {
|
|
return;
|
|
}
|
|
event.respondWith(
|
|
caches.match(event.request).then(response => response || fetch(event.request))
|
|
);
|
|
});
|
|
|
|
// --- Push Event (The missing part!) ---
|
|
self.addEventListener('push', e => {
|
|
try {
|
|
const data = e.data.json();
|
|
self.registration.showNotification(data.title, {
|
|
body: data.body,
|
|
icon: '/icons/icon-192x192.webp'
|
|
});
|
|
} catch (error) {
|
|
console.error('Error in push event:', error);
|
|
self.registration.showNotification('New Notification', {
|
|
body: 'You have a new update.'
|
|
});
|
|
}
|
|
}); |