// Service Worker for PWA support const CACHE_NAME = 'clicker-game-cache-v1'; const urlsToCache = [ '/', '/index.html', '/favicon.svg', '/manifest.json', '/icons/icon-192x192.png', '/icons/icon-512x512.png' ]; // Install the service worker and cache assets self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE_NAME) .then((cache) => { return cache.addAll(urlsToCache); }) ); }); // Activate and clean up old caches self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then((cacheNames) => { return Promise.all( cacheNames.map((cacheName) => { if (cacheName !== CACHE_NAME) { return caches.delete(cacheName); } }) ); }) ); }); // Serve cached content when offline self.addEventListener('fetch', (event) => { event.respondWith( caches.match(event.request) .then((response) => { // Cache hit - return the response from the cached version if (response) { return response; } // Not in cache - fetch from network return fetch(event.request).then( (response) => { // Check if we received a valid response if (!response || response.status !== 200 || response.type !== 'basic') { return response; } // Clone the response const responseToCache = response.clone(); // Add to cache for future use caches.open(CACHE_NAME) .then((cache) => { cache.put(event.request, responseToCache); }); return response; } ); }) ); });