const CACHE_NAME = 'datastack-cache-v3'; const STATIC_ASSETS = [ '/', '/manifest.json', '/icon.svg', '/icon-192.png', '/icon-512.png', '/icon-maskable-512.png', '/apple-touch-icon.png', ]; function buildOfflineShellResponse(pathname) { const safePath = String(pathname || '/').replace(/[<>&"']/g, ''); const html = ` DataStack — Offline
DataStack

Network connection unavailable

Unable to reach ${safePath} while offline. Live ingestion metrics and authenticated admin routes require a connection to the DataStack server.

Overview
`; return new Response(html, { status: 503, statusText: 'Service Unavailable', headers: { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-store', }, }); } self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE_NAME).then((cache) => { return cache.addAll(STATIC_ASSETS).catch(() => {}); }) ); self.skipWaiting(); }); self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then((keys) => { return Promise.all( keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key)) ); }) ); self.clients.claim(); }); self.addEventListener('message', (event) => { if (event.data && event.data.type === 'CLEAR_AUTH_CACHE') { event.waitUntil( caches.open(CACHE_NAME).then(async (cache) => { const requests = await cache.keys(); await Promise.all( requests .filter((req) => { try { const reqUrl = new URL(req.url); return reqUrl.pathname === '/vcon' || reqUrl.pathname.startsWith('/vcon/'); } catch { return false; } }) .map((req) => cache.delete(req)) ); }).catch(() => {}) ); } }); self.addEventListener('fetch', (event) => { const { request } = event; if (request.method !== 'GET') return; let url; try { url = new URL(request.url); } catch { return; } // Only handle same-origin GET requests; never intercept WebSocket, HMR, or API routes if (url.origin !== self.location.origin) return; if ( url.pathname.startsWith('/socket.io/') || url.pathname.startsWith('/api/') || url.pathname.startsWith('/_next/webpack-hmr') ) { return; } // Never cache authenticated /vcon/* responses, but provide an offline HTML shell on PWA launch / navigation failure if (url.pathname === '/vcon' || url.pathname.startsWith('/vcon/')) { if (request.mode !== 'navigate') { return; } event.respondWith( fetch(request).catch(() => buildOfflineShellResponse(url.pathname)) ); return; } event.respondWith( fetch(request) .then((response) => { const cacheControl = response.headers.get('Cache-Control') || ''; const shouldSkipCache = cacheControl.includes('no-store') || cacheControl.includes('private'); if (response && response.ok && response.type === 'basic' && !shouldSkipCache) { const clone = response.clone(); caches.open(CACHE_NAME).then((cache) => cache.put(request, clone)).catch(() => {}); } return response; }) .catch(async () => { const cached = await caches.match(request); if (cached) return cached; if (request.mode === 'navigate') { return buildOfflineShellResponse(url.pathname); } return new Response('Offline — network unavailable.', { status: 503, statusText: 'Service Unavailable', headers: { 'Content-Type': 'text/plain; charset=utf-8' }, }); }) ); });