93 lines
No EOL
2.8 KiB
JavaScript
93 lines
No EOL
2.8 KiB
JavaScript
// RosterHash Service Worker
|
|
const CACHE_NAME = 'rosterhash-v1.0.0';
|
|
const STATIC_CACHE = [
|
|
'/',
|
|
'/static/style.css',
|
|
'/static/manifest.json',
|
|
'/rosterhash_logo.png',
|
|
'/favicon.ico'
|
|
];
|
|
|
|
// Install event - cache static resources
|
|
self.addEventListener('install', event => {
|
|
console.log('RosterHash SW: Installing...');
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => {
|
|
console.log('RosterHash SW: Caching static resources');
|
|
return cache.addAll(STATIC_CACHE);
|
|
})
|
|
.then(() => self.skipWaiting())
|
|
);
|
|
});
|
|
|
|
// Activate event - clean up old caches
|
|
self.addEventListener('activate', event => {
|
|
console.log('RosterHash SW: Activating...');
|
|
event.waitUntil(
|
|
caches.keys().then(cacheNames => {
|
|
return Promise.all(
|
|
cacheNames.map(cacheName => {
|
|
if (cacheName !== CACHE_NAME) {
|
|
console.log('RosterHash SW: Deleting old cache:', cacheName);
|
|
return caches.delete(cacheName);
|
|
}
|
|
})
|
|
);
|
|
}).then(() => self.clients.claim())
|
|
);
|
|
});
|
|
|
|
// Fetch event - network first, fallback to cache for static resources
|
|
self.addEventListener('fetch', event => {
|
|
const { request } = event;
|
|
|
|
// Skip non-GET requests
|
|
if (request.method !== 'GET') return;
|
|
|
|
// For API calls and dynamic content, use network first
|
|
if (request.url.includes('/api/') ||
|
|
request.url.includes('/dashboard') ||
|
|
request.url.includes('/refresh')) {
|
|
event.respondWith(
|
|
fetch(request)
|
|
.catch(() => {
|
|
// If offline, show a basic offline page
|
|
if (request.destination === 'document') {
|
|
return new Response(`
|
|
<html>
|
|
<head>
|
|
<title>RosterHash - Offline</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
</head>
|
|
<body style="font-family: sans-serif; text-align: center; padding: 50px;">
|
|
<h1>You're offline</h1>
|
|
<p>Please check your internet connection and try again.</p>
|
|
<button onclick="window.location.reload()">Retry</button>
|
|
</body>
|
|
</html>
|
|
`, {
|
|
headers: { 'Content-Type': 'text/html' }
|
|
});
|
|
}
|
|
})
|
|
);
|
|
} else {
|
|
// For static resources, cache first
|
|
event.respondWith(
|
|
caches.match(request)
|
|
.then(response => {
|
|
return response || fetch(request)
|
|
.then(response => {
|
|
// Cache successful responses
|
|
if (response.status === 200) {
|
|
const responseClone = response.clone();
|
|
caches.open(CACHE_NAME)
|
|
.then(cache => cache.put(request, responseClone));
|
|
}
|
|
return response;
|
|
});
|
|
})
|
|
);
|
|
}
|
|
}); |