"use strict"; // Önbellek Adı ve Versiyonu const CACHE_NAME = "offline-cache-v1"; const OFFLINE_URL = "/offline.html"; // Önbelleğe Alınacak Dosyalar const filesToCache = [ OFFLINE_URL, "/css/app.css", "/js/app.js", "/images/logo1.png", "/favicon.ico", "/logo72.png", "/logo192.png" ]; // Service Worker'ın Yüklenme (install) Olayı self.addEventListener("install", (event) => { console.log("Service Worker: Installing..."); event.waitUntil( caches.open(CACHE_NAME).then((cache) => { console.log("Service Worker: Caching Files"); return cache.addAll(filesToCache); }).catch((err) => console.error("Service Worker: Caching Failed", err)) ); }); // Ağ İsteklerini Yönetme (fetch) Olayı self.addEventListener("fetch", (event) => { const requestURL = event.request.url; // Sadece http/https isteklerini işleme al if (requestURL.startsWith("http")) { if (event.request.mode === "navigate") { // Sayfa istekleri için event.respondWith( fetch(event.request) .catch(() => caches.match(OFFLINE_URL)) ); } else { // Statik dosyalar için event.respondWith( caches.match(event.request).then((response) => { return response || fetch(event.request).then((fetchResponse) => { // Dinamik olarak önbelleğe ekle return caches.open(CACHE_NAME).then((cache) => { cache.put(event.request, fetchResponse.clone()); return fetchResponse; }); }); }).catch(() => caches.match(OFFLINE_URL)) // Hata durumunda offline sayfası ); } } else { // chrome-extension:// gibi desteklenmeyen URL'leri atla console.warn("Service Worker: Unsupported request ignored", requestURL); } }); // Eski Önbellekleri Temizleme (activate) Olayı self.addEventListener("activate", (event) => { console.log("Service Worker: Activating..."); event.waitUntil( Promise.all([ caches.keys().then((cacheNames) => { return Promise.all( cacheNames.map((cache) => { if (cache !== CACHE_NAME) { console.log("Service Worker: Clearing Old Cache", cache); return caches.delete(cache); } }) ); }), clients.claim() ]) ); }); self.addEventListener('push', function(event) { console.log('Push event received'); if (event.data) { const data = event.data.json(); const options = { body: data.body, icon: '/logo192.png', badge: '/logo72.png', data: { url: data.url }, vibrate: [200, 100, 200], tag: 'notification', requireInteraction: true, renotify: true, actions: [ { action: 'open', title: 'Aç' }, { action: 'close', title: 'Kapat' } ] }; event.waitUntil( self.registration.showNotification(data.title, options) ); } }); self.addEventListener('notificationclick', function(event) { console.log('Notification başlaması lazım..', event); event.notification.close(); if (event.action === 'close') { return; } event.waitUntil( clients.matchAll({type: 'window'}).then(function(clientList) { const url = event.notification.data?.url || '/'; for (const client of clientList) { if (client.url === url && 'focus' in client) { return client.focus(); } } return clients.openWindow(url); }) ); });