drink-it-cold/web/sw.js
torsten b0d90fe191 Anfrageformular: Tarif-Feld entfernt
- Auswahl "Tarif" aus web/index.html entfernt; der Zeitraum ergibt sich aus
  Von/Bis, die Tarifberatung gehoert in die Rueckmeldung
- app/inquiry.php verarbeitet und mailt kein tariff mehr
- Asset-Query auf v=5, Service-Worker-Cache dic-v21
2026-11-29 16:58:49 +01:00

190 lines
5.5 KiB
JavaScript
Raw 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.

/* Drink it cold – Service Worker
*
* Die App braucht für aktuelle Buchungsdaten eine Verbindung. Der Service
* Worker macht sie installierbar und beschleunigt die statischen Assets.
* Authentifizierte HTML-Seiten und API-Antworten werden bewusst NICHT
* zwischengespeichert, damit niemand veraltete Buchungen sieht.
*/
const VERSION = 'dic-v21';
const CACHE = VERSION;
const BASE = new URL(self.registration.scope).pathname.replace(/\/$/, '');
const PRECACHE = [
'/manifest.webmanifest',
'/offline.html',
'/assets/app.css',
'/assets/app.js',
'/assets/logo-mark.svg',
'/assets/favicon.svg',
'/assets/fonts/manrope-latin.woff2',
'/assets/fonts/inter-latin.woff2',
'/assets/img/hero-800.webp',
'/assets/img/hero-1280.webp',
'/assets/icons/icon-192.png',
'/assets/icons/icon-512.png',
'/assets/icons/icon-maskable-192.png',
'/assets/icons/icon-maskable-512.png',
'/assets/icons/apple-touch-icon-180.png'
].map((path) => `${BASE}${path}`);
const offlineUrl = `${BASE}/offline.html`;
const assetPrefix = `${BASE}/assets/`;
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE)
.then((cache) => cache.addAll(PRECACHE))
.then(() => self.skipWaiting())
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys()
.then((keys) => Promise.all(
keys
.filter((key) => key.startsWith('dic-') && key !== CACHE)
.map((key) => caches.delete(key))
))
.then(() => self.clients.claim())
);
});
function cacheKey(request) {
const url = new URL(request.url);
url.search = '';
return url.toString();
}
async function cacheFirst(event, request) {
const cache = await caches.open(CACHE);
const cached = await cache.match(request, { ignoreSearch: true });
const network = fetch(request)
.then((response) => {
if (response && response.ok) {
cache.put(cacheKey(request), response.clone());
}
return response;
})
.catch(() => null);
if (cached) {
event.waitUntil(network);
return cached;
}
return (await network) || new Response('', { status: 504, statusText: 'Offline' });
}
async function assetResponse(request) {
const cache = await caches.open(CACHE);
// Exakter Treffer inklusive Versions-Query. Dadurch wird nach einem
// Deployment sofort die neue Datei geladen statt noch einmal die alte.
const exact = await cache.match(request);
if (exact) {
return exact;
}
try {
const response = await fetch(request);
if (response && response.ok) {
cache.put(request, response.clone());
}
return response;
} catch (error) {
const fallback = await cache.match(request, { ignoreSearch: true });
return fallback || new Response('', { status: 504, statusText: 'Offline' });
}
}
async function navigate(request) {
try {
return await fetch(request);
} catch (error) {
const cache = await caches.open(CACHE);
const offline = await cache.match(offlineUrl);
if (offline) {
return offline;
}
return new Response(
'<!doctype html><html lang="de"><meta charset="utf-8"><title>Offline</title>'
+ '<body style="font-family:system-ui;padding:2rem;background:#f4f9fe;color:#0e1b26">'
+ '<h1>Keine Verbindung</h1><p>Bitte die Internetverbindung prüfen und erneut versuchen.</p></body></html>',
{ status: 503, headers: { 'Content-Type': 'text/html; charset=utf-8' } }
);
}
}
self.addEventListener('fetch', (event) => {
const request = event.request;
if (request.method !== 'GET') {
return;
}
const url = new URL(request.url);
if (url.origin !== self.location.origin) {
return;
}
if (request.mode === 'navigate') {
event.respondWith(navigate(request));
return;
}
if (url.pathname.startsWith(assetPrefix)) {
event.respondWith(assetResponse(request));
return;
}
if (url.pathname === `${BASE}/manifest.webmanifest` || url.pathname === offlineUrl) {
event.respondWith(cacheFirst(event, request));
}
});
/* ---------------------------------------------------------- Web-Push */
self.addEventListener('push', (event) => {
let data = {};
try {
data = event.data ? event.data.json() : {};
} catch (error) {
data = {};
}
const title = data.title || 'Drink it cold – Erinnerung';
const target = data.url || `${BASE}/reminders`;
const options = {
body: data.body || '',
icon: data.icon || `${BASE}/assets/icons/icon-192.png`,
badge: data.badge || `${BASE}/assets/icons/icon-192.png`,
tag: data.tag || 'dic-reminder',
lang: 'de',
data: { url: target },
timestamp: Date.now()
};
event.waitUntil(self.registration.showNotification(title, options));
});
self.addEventListener('notificationclick', (event) => {
event.notification.close();
const raw = (event.notification.data && event.notification.data.url) || `${BASE}/reminders`;
let target;
try {
const url = new URL(raw, self.location.origin);
target = url.origin === self.location.origin ? url.href : `${self.location.origin}${BASE}/reminders`;
} catch (error) {
target = `${self.location.origin}${BASE}/reminders`;
}
event.waitUntil(
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((list) => {
for (const client of list) {
if (new URL(client.url).origin !== self.location.origin) continue;
return client.focus().then(() => {
if ('navigate' in client) return client.navigate(target);
return client;
});
}
return self.clients.openWindow(target);
})
);
});