// MacroPad PWA Service Worker const CACHE_NAME = 'macropad-v3'; const ASSETS_TO_CACHE = [ '/', '/static/css/styles.css', '/static/js/app.js', '/static/icons/icon-192.png', '/static/icons/icon-512.png', '/manifest.json' ]; // Install event - cache assets self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE_NAME) .then((cache) => { console.log('Caching app assets'); return cache.addAll(ASSETS_TO_CACHE); }) .then(() => self.skipWaiting()) ); }); // Activate event - clean old caches self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then((cacheNames) => { return Promise.all( cacheNames .filter((name) => name !== CACHE_NAME) .map((name) => caches.delete(name)) ); }).then(() => self.clients.claim()) ); }); // Fetch event - serve from cache, fallback to network self.addEventListener('fetch', (event) => { const url = new URL(event.request.url); // Always fetch API requests from network if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/ws')) { event.respondWith(fetch(event.request)); return; } // For other requests, try cache first, then network event.respondWith( caches.match(event.request) .then((response) => { if (response) { return response; } return fetch(event.request).then((networkResponse) => { // Cache successful responses if (networkResponse && networkResponse.status === 200) { const responseClone = networkResponse.clone(); caches.open(CACHE_NAME).then((cache) => { cache.put(event.request, responseClone); }); } return networkResponse; }); }) .catch(() => { // Return offline fallback for navigation requests if (event.request.mode === 'navigate') { return caches.match('/'); } }) ); });