feat: add PWA and offline mode support with local caching and action safeguards

This commit is contained in:
tyuXX 2026-10-21 20:26:24 +00:00
parent 039b5ac483
commit 128a5f7553
15 changed files with 589 additions and 0 deletions

122
public/sw.js Normal file
View file

@ -0,0 +1,122 @@
const CACHE_NAME = 'bekultur-v1';
const STATIC_ASSETS = [
'/',
'/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);
// Ignore cross-origin external analytics or fonts
if (url.origin !== self.location.origin) {
return;
}
// 1. Static Assets (CSS, JS, Icons, Images) -> Cache-First
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) {
return cachedResponse;
}
return 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;
});
})
);
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
const cached = await caches.match(req);
if (cached) {
return cached;
}
// If it was a navigation request, try fallback to cached dashboard or root
if (req.mode === 'navigate') {
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' }
});
})
);
});