fixes and patches n stuff
This commit is contained in:
parent
7c7ae8e051
commit
1af973d001
14 changed files with 571 additions and 268 deletions
47
public/sw.js
47
public/sw.js
|
|
@ -1,7 +1,11 @@
|
|||
const CACHE_NAME = 'bekultur-v1';
|
||||
const CACHE_NAME = 'bekultur-v2';
|
||||
|
||||
const STATIC_ASSETS = [
|
||||
'/',
|
||||
'/dashboard',
|
||||
'/syllabus',
|
||||
'/private-lessons',
|
||||
'/exams',
|
||||
'/css/style.css',
|
||||
'/js/pwa.js',
|
||||
'/js/app.js',
|
||||
|
|
@ -53,13 +57,19 @@ self.addEventListener('fetch', (event) => {
|
|||
|
||||
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) {
|
||||
if (url.origin !== self.location.origin && !isQuestionImage) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. Static Assets (CSS, JS, Icons, Images) -> Cache-First
|
||||
// 1. Static Assets & Question Images -> Cache-First
|
||||
const isStatic =
|
||||
isQuestionImage ||
|
||||
url.pathname.startsWith('/css/') ||
|
||||
url.pathname.startsWith('/js/') ||
|
||||
url.pathname.startsWith('/icons/') ||
|
||||
|
|
@ -75,11 +85,14 @@ self.addEventListener('fetch', (event) => {
|
|||
return cachedResponse;
|
||||
}
|
||||
return fetch(req).then((networkResponse) => {
|
||||
if (networkResponse && networkResponse.status === 200) {
|
||||
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' });
|
||||
});
|
||||
})
|
||||
);
|
||||
|
|
@ -98,13 +111,35 @@ self.addEventListener('fetch', (event) => {
|
|||
})
|
||||
.catch(async () => {
|
||||
// Network failed (Offline) -> Look in CacheStorage
|
||||
const cached = await caches.match(req);
|
||||
// 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 fallback to cached dashboard or root
|
||||
// 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;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue