// SnapCAD Studio Progressive Web App (PWA) Service Worker const CACHE_NAME = "snapcad-v1.2.2"; const PRECACHE_ASSETS = [ "/", "/manifest.json", "/icons/icon-192.png", "/icons/icon-512.png", "/icons/icon-maskable-192.png", "/icons/icon-maskable-512.png", "/icons/apple-touch-icon.png", ]; // Ağ hatasında her zaman geçerli bir Response döndür. respondWith(undefined) // "Failed to convert value to 'Response'" hatasına ve boş sayfaya yol açar. function gatewayTimeoutResponse() { return new Response("", { status: 504, statusText: "Gateway Timeout" }); } function offlinePageResponse() { return new Response( "SnapCAD Studio çevrimdışı" + "

Bağlantı kurulamadı

" + "

SnapCAD Studio şu anda yüklenemedi. İnternet bağlantınızı kontrol edip sayfayı yenileyin.

", { status: 503, statusText: "Offline", headers: { "Content-Type": "text/html; charset=utf-8" } }, ); } // Install Event - Pre-cache essential app shell self.addEventListener("install", (event) => { event.waitUntil( caches .open(CACHE_NAME) .then((cache) => { return cache.addAll(PRECACHE_ASSETS).catch((err) => { console.warn("[SnapCAD SW] Precache warning:", err); }); }) .then(() => self.skipWaiting()), ); }); // Activate Event - Clean up stale cache versions self.addEventListener("activate", (event) => { event.waitUntil( caches .keys() .then((keys) => { return Promise.all( keys .filter((key) => key !== CACHE_NAME) .map((key) => caches.delete(key)), ); }) .then(() => self.clients.claim()), ); }); // Fetch Event - Resilient caching strategies self.addEventListener("fetch", (event) => { const { request } = event; const url = new URL(request.url); // Never cache non-GET requests or backend API endpoints if ( request.method !== "GET" || url.pathname.startsWith("/api/") || url.pathname.endsWith(".php") ) { return; } // Cross-origin istekleri (ör. Cloudflare Analytics beacon) service worker'a // sokma; aksi hâlde önbellekte olmayan istekler undefined döndürür. if (url.origin !== self.location.origin) { return; } // Strategy 1: Cache-First for Immutable WebAssembly Runtimes if ( url.pathname.endsWith(".wasm") || url.pathname.includes("/occt/") || url.pathname.includes("/dwgdxf/") || url.pathname.includes("/libredwg/") ) { event.respondWith( caches.match(request).then(async (cached) => { if (cached) return cached; try { const response = await fetch(request); if (response && response.status === 200) { const clone = response.clone(); caches.open(CACHE_NAME).then((cache) => cache.put(request, clone)); } return response; } catch { return gatewayTimeoutResponse(); } }), ); return; } // Strategy 2: Stale-While-Revalidate for Static Assets (_next/static, icons, images) if ( url.pathname.startsWith("/_next/static/") || url.pathname.startsWith("/icons/") || url.pathname.startsWith("/assets/") ) { event.respondWith( caches.match(request).then(async (cached) => { const fetchPromise = fetch(request) .then((networkResponse) => { if (networkResponse && networkResponse.status === 200) { const clone = networkResponse.clone(); caches.open(CACHE_NAME).then((cache) => cache.put(request, clone)); } return networkResponse; }) .catch(() => cached || gatewayTimeoutResponse()); return cached || fetchPromise; }), ); return; } // Strategy 3: Network-First with Offline Fallback for HTML Pages / Navigation if (request.mode === "navigate") { event.respondWith( fetch(request) .then((response) => { if (response && response.status === 200) { const clone = response.clone(); caches.open(CACHE_NAME).then((cache) => cache.put(request, clone)); } return response; }) .catch(async () => { const cached = await caches.match(request); if (cached) return cached; const fallback = await caches.match("/"); return fallback || offlinePageResponse(); }), ); return; } // Default: Network with Cache Fallback event.respondWith( fetch(request).catch(async () => { const cached = await caches.match(request); return cached || gatewayTimeoutResponse(); }), ); }); // Listen for message to skip waiting on update self.addEventListener("message", (event) => { if (event.data && event.data.type === "SKIP_WAITING") { self.skipWaiting(); } });