/// declare const self: ServiceWorkerGlobalScope; import { clientsClaim } from 'workbox-core'; import { cleanupOutdatedCaches, precacheAndRoute } from 'workbox-precaching'; import { registerRoute } from 'workbox-routing'; import { StaleWhileRevalidate, CacheFirst, NetworkFirst } from 'workbox-strategies'; import { ExpirationPlugin } from 'workbox-expiration'; import { CacheableResponsePlugin } from 'workbox-cacheable-response'; // Precache all assets generated by Vite build precacheAndRoute(self.__WB_MANIFEST); cleanupOutdatedCaches(); // Cache Google Fonts stylesheets registerRoute( ({ url }) => url.origin === 'https://fonts.googleapis.com', new StaleWhileRevalidate({ cacheName: 'google-fonts-stylesheets', }) ); // Cache Google Fonts webfont files with 1-year expiration registerRoute( ({ url }) => url.origin === 'https://fonts.gstatic.com', new CacheFirst({ cacheName: 'google-fonts-webfonts', plugins: [ new CacheableResponsePlugin({ statuses: [0, 200], }), new ExpirationPlugin({ maxAgeSeconds: 60 * 60 * 24 * 365, maxEntries: 30, }), ], }) ); // Cache static images and icons registerRoute( ({ request }) => request.destination === 'image', new CacheFirst({ cacheName: 'images-cache', plugins: [ new CacheableResponsePlugin({ statuses: [0, 200], }), new ExpirationPlugin({ maxEntries: 60, maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days }), ], }) ); // Force immediate activation of new SW self.addEventListener('install', () => { console.log('[SW] Installing Cognitive Clarity PWA Service Worker...'); self.skipWaiting(); }); self.addEventListener('activate', () => { console.log('[SW] Activating Cognitive Clarity PWA Service Worker...'); clientsClaim(); }); // Handle explicit SKIP_WAITING message from client UI self.addEventListener('message', (event) => { if (event.data?.type === 'SKIP_WAITING') { self.skipWaiting(); } });