/** * Backup Service Worker für MYP Platform * Vereinfachte Offline-Funktionalität als Fallback */ const CACHE_NAME = 'myp-platform-backup-v1'; // Assets die gecacht werden sollen const ASSETS_TO_CACHE = [ '/', '/dashboard', '/static/css/tailwind.min.css', '/static/css/tailwind-dark.min.css', '/static/js/ui-components.js', '/static/js/offline-app.js', '/static/favicon.ico' ]; // Install Event - Cache core assets self.addEventListener('install', (event) => { console.log('Backup SW: Installing...'); event.waitUntil( caches.open(CACHE_NAME) .then((cache) => { console.log('Backup SW: Caching assets'); return cache.addAll(ASSETS_TO_CACHE); }) .then(() => { console.log('Backup SW: Assets cached'); return self.skipWaiting(); }) .catch((error) => { console.error('Backup SW: Error caching assets', error); }) ); }); // Activate Event - Clean up old caches self.addEventListener('activate', (event) => { console.log('Backup SW: Activating...'); event.waitUntil( caches.keys() .then((cacheNames) => { return Promise.all( cacheNames.map((cacheName) => { if (cacheName !== CACHE_NAME) { console.log('Backup SW: Deleting old cache', cacheName); return caches.delete(cacheName); } }) ); }) .then(() => { console.log('Backup SW: Activated'); return self.clients.claim(); }) ); }); // Fetch Event - Cache first, network fallback mit korrekter Response-Behandlung self.addEventListener('fetch', (event) => { // Nur GET-Requests und HTTP/HTTPS unterstützen if (event.request.method !== 'GET' || (!event.request.url.startsWith('http:') && !event.request.url.startsWith('https:'))) { return; } event.respondWith( handleRequest(event.request) ); }); // Request-Handler mit korrekter Response-Behandlung async function handleRequest(request) { try { // Zuerst im Cache suchen const cachedResponse = await caches.match(request); if (cachedResponse) { return cachedResponse; } // Netzwerk-Request versuchen const networkResponse = await fetch(request); // Nur erfolgreiche Responses cachen if (networkResponse && networkResponse.ok && networkResponse.type === 'basic') { const cache = await caches.open(CACHE_NAME); cache.put(request, networkResponse.clone()); } return networkResponse; } catch (error) { console.error('Backup SW: Request failed', error); // Cache-Fallback versuchen const cachedResponse = await caches.match(request); if (cachedResponse) { return cachedResponse; } // Offline-Fallback Response return new Response('Offline - Service nicht verfügbar', { status: 503, statusText: 'Service Unavailable', headers: { 'Content-Type': 'text/plain; charset=utf-8' } }); } } console.log('Backup Service Worker: Script loaded successfully');