/* İhaleist service worker — uygulama kabuğu çevrimdışı açılır, API her zaman ağdan. Sürüm değişince eski önbellek temizlenir (build her deploy'da yeni asset adı üretir). */ const SURUM = 'ihaleist-v1'; const KABUK = ['/', '/manifest.webmanifest', '/icons/icon-192.png', '/icons/icon-512.png']; self.addEventListener('install', (e) => { e.waitUntil(caches.open(SURUM).then((c) => c.addAll(KABUK)).then(() => self.skipWaiting())); }); self.addEventListener('activate', (e) => { e.waitUntil(caches.keys().then((ks) => Promise.all(ks.filter((k) => k !== SURUM).map((k) => caches.delete(k)))).then(() => self.clients.claim())); }); self.addEventListener('fetch', (e) => { const url = new URL(e.request.url); if (e.request.method !== 'GET' || url.origin !== self.location.origin) return; // API: yalnız ağ — önbellekten eski ihale gösterme if (url.pathname.startsWith('/api/')) return; // Hash'li build varlıkları: cache-first (değişmez adlar) if (url.pathname.startsWith('/assets/')) { e.respondWith(caches.match(e.request).then((hit) => hit || fetch(e.request).then((r) => { const kopya = r.clone(); caches.open(SURUM).then((c) => c.put(e.request, kopya)); return r; }))); return; } // Kabuk / sayfa: network-first, ağ yoksa önbellekten index e.respondWith(fetch(e.request).then((r) => { const kopya = r.clone(); caches.open(SURUM).then((c) => c.put(e.request, kopya)); return r; }).catch(() => caches.match(e.request).then((hit) => hit || caches.match('/')))); });