87 lines
2 KiB
JavaScript
87 lines
2 KiB
JavaScript
|
|
const CACHE_NAME = 'tcg-vault-v1';
|
||
|
|
const urlsToCache = [
|
||
|
|
'/',
|
||
|
|
'/static/js/bundle.js',
|
||
|
|
'/static/css/main.css',
|
||
|
|
'/manifest.json',
|
||
|
|
'/favicon.ico',
|
||
|
|
'/logo192.png',
|
||
|
|
'/logo512.png'
|
||
|
|
];
|
||
|
|
|
||
|
|
// Install event
|
||
|
|
self.addEventListener('install', (event) => {
|
||
|
|
event.waitUntil(
|
||
|
|
caches.open(CACHE_NAME)
|
||
|
|
.then((cache) => cache.addAll(urlsToCache))
|
||
|
|
.catch((error) => {
|
||
|
|
console.error('Failed to cache resources:', error);
|
||
|
|
})
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
// Fetch event
|
||
|
|
self.addEventListener('fetch', (event) => {
|
||
|
|
event.respondWith(
|
||
|
|
caches.match(event.request)
|
||
|
|
.then((response) => {
|
||
|
|
// Return cached version or fetch from network
|
||
|
|
return response || fetch(event.request);
|
||
|
|
})
|
||
|
|
.catch((error) => {
|
||
|
|
console.error('Fetch failed:', error);
|
||
|
|
// Return offline page if available
|
||
|
|
return caches.match('/');
|
||
|
|
})
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
// Activate event
|
||
|
|
self.addEventListener('activate', (event) => {
|
||
|
|
event.waitUntil(
|
||
|
|
caches.keys().then((cacheNames) => {
|
||
|
|
return Promise.all(
|
||
|
|
cacheNames.map((cacheName) => {
|
||
|
|
if (cacheName !== CACHE_NAME) {
|
||
|
|
return caches.delete(cacheName);
|
||
|
|
}
|
||
|
|
})
|
||
|
|
);
|
||
|
|
})
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
// Background sync for offline actions
|
||
|
|
self.addEventListener('sync', (event) => {
|
||
|
|
if (event.tag === 'background-sync') {
|
||
|
|
event.waitUntil(
|
||
|
|
// Handle offline actions when back online
|
||
|
|
console.log('Background sync triggered')
|
||
|
|
);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// Push notifications (for future use)
|
||
|
|
self.addEventListener('push', (event) => {
|
||
|
|
if (event.data) {
|
||
|
|
const data = event.data.json();
|
||
|
|
const options = {
|
||
|
|
body: data.body,
|
||
|
|
icon: '/logo192.png',
|
||
|
|
badge: '/favicon.ico',
|
||
|
|
vibrate: [200, 100, 200]
|
||
|
|
};
|
||
|
|
|
||
|
|
event.waitUntil(
|
||
|
|
self.registration.showNotification(data.title, options)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// Notification click
|
||
|
|
self.addEventListener('notificationclick', (event) => {
|
||
|
|
event.notification.close();
|
||
|
|
event.waitUntil(
|
||
|
|
clients.openWindow('/')
|
||
|
|
);
|
||
|
|
});
|