Files
ihaleist/public/sw.js
T
hikmettanandClaude Fable 5.1 909ebae7c2 fix(pwa): önbellek v2 — yanlış MIME ile saklanan pdf worker temizlenir, bozuk JS yanıtları artık önbelleğe alınmaz
nginx düzeltmesi tek başına yetmezdi: servis çalışanı /assets/ dosyalarını
cache-first sakladığı için kullanıcıların tarayıcısındaki octet-stream'li
worker kopyası redeploy sonrasında da kullanılmaya devam ederdi. Sürüm
yükseltilince eski önbellek silinir; ayrıca .js/.mjs dosyaları yalnızca
JavaScript MIME tipiyle ve 200 ile geldiğinde saklanır.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-06 01:02:35 +03:00

37 lines
2.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* İ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-v2'; // v2: yanlış MIME ile saklanmış pdf worker kopyaları temizlensin
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) => {
// Yalnızca sağlıklı yanıt saklanır: 404 veya JavaScript dosyasının
// yanlış MIME tipiyle (octet-stream) gelmesi önbelleğe girerse
// sunucu düzeltilse bile kullanıcı eski bozuk kopyayı görmeye devam eder.
const tip = r.headers.get('content-type') || '';
const jsMi = /\.m?js$/.test(url.pathname);
if (r.ok && (!jsMi || /javascript|ecmascript/i.test(tip))) {
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('/'))));
});