const CACHE_NAME = 'bekultur-v4'; const STATIC_ASSETS = [ '/', '/dashboard', '/syllabus', '/private-lessons', '/exams', '/css/style.css', '/js/pwa.js', '/js/app.js', '/js/syllabus.js', '/js/private-lessons.js', '/js/exams.js', '/manifest.json', '/icons/icon.svg', '/icons/icon-192.png', '/icons/icon-512.png' ]; // Pre-cache core assets self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE_NAME).then((cache) => { return cache.addAll(STATIC_ASSETS).catch((err) => { console.warn('Some static assets failed to pre-cache:', err); }); }) ); self.skipWaiting(); }); // Clean up old caches on activation self.addEventListener('activate', (event) => { event.waitUntil( caches.keys().then((keys) => { return Promise.all( keys.map((key) => { if (key !== CACHE_NAME) { return caches.delete(key); } }) ); }) ); self.clients.claim(); }); // Fetch handler self.addEventListener('fetch', (event) => { const req = event.request; // Only handle GET requests if (req.method !== 'GET') { return; } const url = new URL(req.url); // Check if this is an E-Kültür portal image (either direct or via same-origin proxy) const isDirectPortalAsset = url.hostname === 'ozelbursakulturokullari.com' || url.hostname.endsWith('.ozelbursakulturokullari.com'); const isProxiedImage = url.origin === self.location.origin && url.pathname === '/api/image-proxy'; // Ignore unrelated cross-origin requests if (url.origin !== self.location.origin && !isDirectPortalAsset) { return; } // 1. Portal Images (Direct or Proxied) -> Route via /api/image-proxy, Cache Verified 200 OK Images if (isDirectPortalAsset || isProxiedImage) { const proxyUrl = isDirectPortalAsset ? `${self.location.origin}/api/image-proxy?url=${encodeURIComponent(req.url)}` : req.url; const proxyReq = new Request(proxyUrl, { credentials: 'same-origin' }); event.respondWith( caches.match(proxyReq).then((cachedResponse) => { if (cachedResponse && cachedResponse.status === 200) { return cachedResponse; } return fetch(proxyReq) .then((networkResponse) => { if (networkResponse && networkResponse.ok && networkResponse.status === 200) { const clone = networkResponse.clone(); caches.open(CACHE_NAME).then((cache) => cache.put(proxyReq, clone)); } return networkResponse; }) .catch(async () => { const fallback = await caches.match(proxyReq); if (fallback) return fallback; return new Response('', { status: 404, statusText: 'Image unavailable offline' }); }); }) ); return; } // 2. Local Static Assets -> Cache-First (only caching verified 200 OK responses) const isStatic = url.pathname.startsWith('/css/') || url.pathname.startsWith('/js/') || url.pathname.startsWith('/icons/') || url.pathname === '/manifest.json' || url.pathname.endsWith('.png') || url.pathname.endsWith('.svg') || url.pathname.endsWith('.ico'); if (isStatic) { event.respondWith( caches.match(req).then((cachedResponse) => { if (cachedResponse && cachedResponse.status === 200) { return cachedResponse; } return fetch(req).then((networkResponse) => { if (networkResponse && networkResponse.ok && networkResponse.status === 200) { const clone = networkResponse.clone(); caches.open(CACHE_NAME).then((cache) => cache.put(req, clone)); } return networkResponse; }); }) ); return; } // 3. Navigation Pages & API GETs -> Network-First with Cache Fallback event.respondWith( fetch(req) .then((networkResponse) => { if (networkResponse && networkResponse.status === 200) { const clone = networkResponse.clone(); caches.open(CACHE_NAME).then((cache) => cache.put(req, clone)); } return networkResponse; }) .catch(async () => { // Network failed (Offline) -> Look in CacheStorage // 1. Try exact match let cached = await caches.match(req); // 2. Try match ignoring query parameters if (!cached) { cached = await caches.match(req, { ignoreSearch: true }); } // 3. Try match by pathname if (!cached) { cached = await caches.match(url.pathname); } if (cached) { return cached; } // If it was a navigation request, try route-specific fallbacks if (req.mode === 'navigate') { if (url.pathname.startsWith('/exams')) { const fallback = await caches.match('/exams'); if (fallback) return fallback; } if (url.pathname.startsWith('/syllabus')) { const fallback = await caches.match('/syllabus'); if (fallback) return fallback; } if (url.pathname.startsWith('/private-lessons')) { const fallback = await caches.match('/private-lessons'); if (fallback) return fallback; } const fallbackDashboard = await caches.match('/dashboard'); if (fallbackDashboard) return fallbackDashboard; const fallbackRoot = await caches.match('/'); if (fallbackRoot) return fallbackRoot; } return new Response('Offline: Content not available in cache.', { status: 503, statusText: 'Service Unavailable', headers: { 'Content-Type': 'text/plain; charset=utf-8' } }); }) ); });