BeKultur/public/sw.js
2026-10-21 21:07:54 +00:00

157 lines
4.6 KiB
JavaScript

const CACHE_NAME = 'bekultur-v2';
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);
// Allow caching question images from panel.ozelbursakulturokullari.com
const isQuestionImage =
url.origin.includes('ozelbursakulturokullari.com') &&
(url.pathname.endsWith('.png') || url.pathname.endsWith('.jpg') || url.pathname.endsWith('.jpeg') || url.pathname.includes('/soru/'));
// Ignore cross-origin external analytics or fonts
if (url.origin !== self.location.origin && !isQuestionImage) {
return;
}
// 1. Static Assets & Question Images -> Cache-First
const isStatic =
isQuestionImage ||
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) {
return cachedResponse;
}
return fetch(req).then((networkResponse) => {
if (networkResponse && (networkResponse.status === 200 || networkResponse.type === 'opaque')) {
const clone = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(req, clone));
}
return networkResponse;
}).catch(() => {
// If question image fails offline and not cached, return empty placeholder
return new Response('', { status: 404, statusText: 'Not found offline' });
});
})
);
return;
}
// 2. 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' }
});
})
);
});