1const CACHE_PREFIX = 'zadikim-'; 2const CACHE_NAME = `${CACHE_PREFIX}v7`; 3const URLS_TO_CACHE = [ 4 '/manifest.json', 5 '/favicon.ico', 6 '/favicon-32x32.png', 7 '/favicon-16x16.png', 8 '/apple-touch-icon.png', 9 '/android-chrome-192x192.png', 10 '/android-chrome-512x512.png' 11]; 12 13const CACHEABLE_DESTINATIONS = new Set([ 14 'font', 15 'image', 16 'manifest', 17 'script', 18 'style' 19]); 20 21const IS_LOCAL_DEVELOPMENT = ['localhost', '127.0.0.1', '[::1]'] 22 .includes(self.location.hostname); 23 24function isSupportedSameOriginRequest(request, url) { 25 return request.method === 'GET' && 26 (url.protocol === 'http:' || url.protocol === 'https:') && 27 url.origin === self.location.origin; 28} 29 30function canCacheResponse(request, response) { 31 if ( 32 !response.ok || 33 response.status !== 200 || 34 response.redirected || 35 (response.type !== 'basic' && response.type !== 'cors') 36 ) { 37 return false; 38 } 39 40 // Never cache SPA HTML under script/style URLs (CDN soft-404 poison). 41 const contentType = response.headers.get('content-type') || ''; 42 if (request.destination === 'script' && !/javascript|ecmascript|wasm/i.test(contentType)) { 43 return false; 44 } 45 if (request.destination === 'style' && !/text\/css/i.test(contentType)) { 46 return false; 47 } 48 49 return true; 50} 51 52async function cacheResponse(request, response) { 53 if (!canCacheResponse(request, response)) return; 54 55 const cache = await caches.open(CACHE_NAME); 56 await cache.put(request, response); 57} 58 59async function handleStaticAsset(request) { 60 const cachedResponse = await caches.match(request); 61 if (cachedResponse) { 62 const cachedType = cachedResponse.headers.get('content-type') || ''; 63 const looksLikeHtml = /text\/html/i.test(cachedType); 64 if (!looksLikeHtml) return cachedResponse; 65 } 66 67 const networkResponse = await fetch(request, { redirect: 'follow' }); 68 if (canCacheResponse(request, networkResponse)) { 69 try { 70 await cacheResponse(request, networkResponse.clone()); 71 } catch (error) { 72 console.warn('Could not cache asset:', error); 73 } 74 } 75 76 return networkResponse; 77} 78 79self.addEventListener('install', (event) => { 80 event.waitUntil( 81 (async () => { 82 // Vite development modules must never be served through Cache Storage. 83 if (!IS_LOCAL_DEVELOPMENT) { 84 const cache = await caches.open(CACHE_NAME); 85 await cache.addAll(URLS_TO_CACHE); 86 } 87 await self.skipWaiting(); 88 })() 89 ); 90}); 91 92self.addEventListener('activate', (event) => { 93 event.waitUntil( 94 (async () => { 95 const cacheNames = await caches.keys(); 96 await Promise.all( 97 cacheNames 98 .filter((cacheName) => 99 cacheName.startsWith(CACHE_PREFIX) && cacheName !== CACHE_NAME 100 ) 101 .map((cacheName) => caches.delete(cacheName)) 102 ); 103 await self.clients.claim(); 104 })() 105 ); 106}); 107 108self.addEventListener('fetch', (event) => { 109 // A previously installed worker can still briefly control localhost while it 110 // is being unregistered. Let the browser handle every development request. 111 if (IS_LOCAL_DEVELOPMENT) return; 112 113 const { request } = event; 114 let url; 115 116 try { 117 url = new URL(request.url); 118 } catch { 119 return; 120 } 121 122 // Cache Storage only accepts HTTP(S) requests. This also excludes requests 123 // injected by browser extensions and all cross-origin resources. 124 if (!isSupportedSameOriginRequest(request, url)) return; 125 126 // Let the browser handle navigations so HTTP redirects retain their original 127 // redirect mode. Returning a followed redirect through respondWith() makes 128 // Chrome reject legacy URLs such as /?p=254 with ERR_FAILED. 129 if (request.mode === 'navigate') { 130 return; 131 } 132 133 // API/auth responses can be private or user-specific and must never be put 134 // into the shared application cache. 135 if (url.pathname.startsWith('/api/')) return; 136 137 if (!CACHEABLE_DESTINATIONS.has(request.destination)) return; 138 139 event.respondWith(handleStaticAsset(request)); 140});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.