|
| 1 | +/** |
| 2 | + * Zenly SlideClean - Powerful Service Worker |
| 3 | + * Advanced caching for maximum performance |
| 4 | + */ |
| 5 | + |
| 6 | +const CACHE_NAME = 'zenly-slideclean-v1'; |
| 7 | +const CACHE_VERSION = 1; |
| 8 | + |
| 9 | +// All assets to pre-cache on install |
| 10 | +const PRECACHE_ASSETS = [ |
| 11 | + '/', |
| 12 | + '/index.html', |
| 13 | + '/manifest.json', |
| 14 | + '/robots.txt', |
| 15 | + '/sitemap.xml', |
| 16 | + '/llm.txt', |
| 17 | + '/llms-full.txt', |
| 18 | + // Images |
| 19 | + '/assets/favicon.png', |
| 20 | + '/assets/og-image.png', |
| 21 | + // Core Libraries |
| 22 | + '/assets/lib/pdf.min.js', |
| 23 | + '/assets/lib/jspdf.umd.min.js', |
| 24 | + '/assets/lib/pdf.worker.min.js', |
| 25 | + // Fonts CSS |
| 26 | + '/assets/fonts/fonts.css', |
| 27 | + // Inter Font Files (Latin) |
| 28 | + '/assets/fonts/UcC73FwrK3iLTeHuS_nVMrMxCp50SjIa1ZL7.woff2', |
| 29 | + '/assets/fonts/UcC73FwrK3iLTeHuS_nVMrMxCp50SjIa25L7SUc.woff2', |
| 30 | + // JetBrains Mono Font Files (Latin) |
| 31 | + '/assets/fonts/tDbv2o-flEEny0FZhsfKu5WU4zr3E_BX0PnT8RD8yKwBNntkaToggR7BYRbKPxDcwg.woff2' |
| 32 | +]; |
| 33 | + |
| 34 | +// Install: Pre-cache all critical assets |
| 35 | +self.addEventListener('install', (event) => { |
| 36 | + console.log('[SW] Installing v' + CACHE_VERSION); |
| 37 | + event.waitUntil( |
| 38 | + caches.open(CACHE_NAME) |
| 39 | + .then((cache) => { |
| 40 | + console.log('[SW] Pre-caching assets'); |
| 41 | + return cache.addAll(PRECACHE_ASSETS); |
| 42 | + }) |
| 43 | + .then(() => self.skipWaiting()) |
| 44 | + ); |
| 45 | +}); |
| 46 | + |
| 47 | +// Activate: Clean up old caches |
| 48 | +self.addEventListener('activate', (event) => { |
| 49 | + console.log('[SW] Activated v' + CACHE_VERSION); |
| 50 | + event.waitUntil( |
| 51 | + caches.keys().then((cacheNames) => { |
| 52 | + return Promise.all( |
| 53 | + cacheNames.map((cache) => { |
| 54 | + if (cache !== CACHE_NAME) { |
| 55 | + console.log('[SW] Deleting old cache:', cache); |
| 56 | + return caches.delete(cache); |
| 57 | + } |
| 58 | + }) |
| 59 | + ); |
| 60 | + }).then(() => self.clients.claim()) |
| 61 | + ); |
| 62 | +}); |
| 63 | + |
| 64 | +// Fetch: Cache-First with Network Fallback (for max speed) |
| 65 | +self.addEventListener('fetch', (event) => { |
| 66 | + // Skip non-GET requests |
| 67 | + if (event.request.method !== 'GET') return; |
| 68 | + |
| 69 | + event.respondWith( |
| 70 | + caches.match(event.request).then((cachedResponse) => { |
| 71 | + if (cachedResponse) { |
| 72 | + // Return cached version immediately |
| 73 | + // Also fetch from network to update cache in background |
| 74 | + fetch(event.request).then((networkResponse) => { |
| 75 | + if (networkResponse && networkResponse.status === 200) { |
| 76 | + caches.open(CACHE_NAME).then((cache) => { |
| 77 | + cache.put(event.request, networkResponse); |
| 78 | + }); |
| 79 | + } |
| 80 | + }).catch(() => { }); |
| 81 | + return cachedResponse; |
| 82 | + } |
| 83 | + |
| 84 | + // Not in cache, fetch from network |
| 85 | + return fetch(event.request).then((networkResponse) => { |
| 86 | + // Cache the new response for future |
| 87 | + if (networkResponse && networkResponse.status === 200) { |
| 88 | + const responseClone = networkResponse.clone(); |
| 89 | + caches.open(CACHE_NAME).then((cache) => { |
| 90 | + cache.put(event.request, responseClone); |
| 91 | + }); |
| 92 | + } |
| 93 | + return networkResponse; |
| 94 | + }).catch(() => { |
| 95 | + // Offline fallback for HTML pages |
| 96 | + if (event.request.headers.get('accept').includes('text/html')) { |
| 97 | + return caches.match('/index.html'); |
| 98 | + } |
| 99 | + }); |
| 100 | + }) |
| 101 | + ); |
| 102 | +}); |
0 commit comments