// public/sw.js const CACHE_NAME = 'app-cache-v1'; const ASSETS_TO_CACHE = [ '/', '/manifest.json', // Add paths to your main CSS and JS files here // e.g., '/build/app.css', '/build/app.js' ]; // Install Event: Cache core static assets self.addEventListener('install', event => { event.waitUntil( caches.open(CACHE_NAME) .then(cache => { return cache.addAll(ASSETS_TO_CACHE); }) ); self.skipWaiting(); }); // Activate Event: Clean up old caches if you change the CACHE_NAME version self.addEventListener('activate', event => { event.waitUntil( caches.keys().then(cacheNames => { return Promise.all( cacheNames.map(cache => { if (cache !== CACHE_NAME) { return caches.delete(cache); } }) ); }) ); self.clients.claim(); }); // Fetch Event: Network First, Fallback to Cache self.addEventListener('fetch', event => { // Only cache GET requests if (event.request.method !== 'GET') return; event.respondWith( fetch(event.request) .then(networkResponse => { // If the network request succeeds, clone it and put it in the cache return caches.open(CACHE_NAME).then(cache => { cache.put(event.request, networkResponse.clone()); return networkResponse; }); }) .catch(() => { // If the network fails (offline), try to serve from cache return caches.match(event.request); }) ); });