Files
vibing/bibel-pwa/service-worker.js
thrhymes b049dded72 feat: Add WFW-Aushang web app with PWA support, offline caching, and dark mode
- Created index.html for the main application interface with responsive design and dark mode support.
- Added manifest.webmanifest for PWA configuration, including app icons and display settings.
- Implemented service worker (sw.js) for offline caching of assets and network-first strategy for versioning.
- Introduced version.json to manage app versioning.
2025-12-24 16:59:51 +01:00

69 lines
1.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* Service Worker für die Bibel LernApp
* Dieser Worker sorgt dafür, dass die wesentlichen Dateien
* für den OfflineBetrieb zwischengespeichert werden und
* veraltetete Caches bereinigt werden.
*/
const CACHE_NAME = 'bibel-app-cache-v3';
// Arbeite mit relativen Pfaden, damit die PWA auch aus einem Unterordner funktioniert.
const BASE_PATH = new URL(self.registration.scope).pathname.replace(/\/$/, '');
const scopePath = (path) => `${BASE_PATH}/${path}`;
const OFFLINE_URLS = [
'index.html',
'styles.css',
'script.js',
'manifest.json',
'data.json',
'data.csv',
'icon-192x192.png',
'icon-512x512.png'
].map(scopePath);
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(OFFLINE_URLS);
})
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
})
);
});
self.addEventListener('fetch', (event) => {
if (event.request.method !== 'GET') return;
const requestUrl = new URL(event.request.url);
// Nur Anfragen innerhalb des eigenen Ursprungs beantworten, damit OffsiteRequests
// nicht ungewollt überschrieben werden.
if (requestUrl.origin !== self.location.origin) return;
event.respondWith(
caches.match(event.request).then((cached) => {
if (cached) return cached;
return fetch(event.request).catch(() => {
// Fallback bei HTMLAnfragen
if (
event.request.mode === 'navigate' ||
event.request.headers.get('accept')?.includes('text/html')
) {
return caches.match(scopePath('index.html'));
}
});
})
);
});