// GameTime Service Worker const CACHE_NAME = 'gametime-v1.0.0'; const STATIC_CACHE = [ '/', '/static/style.css', '/static/manifest.json', '/gametime_logo.png', '/favicon.ico' ]; // Install event - cache static resources self.addEventListener('install', event => { console.log('GameTime SW: Installing...'); event.waitUntil( caches.open(CACHE_NAME) .then(cache => { console.log('GameTime SW: Caching static resources'); return cache.addAll(STATIC_CACHE); }) .then(() => self.skipWaiting()) ); }); // Activate event - clean up old caches self.addEventListener('activate', event => { console.log('GameTime SW: Activating...'); event.waitUntil( caches.keys().then(cacheNames => { return Promise.all( cacheNames.map(cacheName => { if (cacheName !== CACHE_NAME) { console.log('GameTime 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(` GameTime - Offline

You're offline

Please check your internet connection and try again.

`, { 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; }); }) ); } });