141 lines
3.9 KiB
JavaScript
141 lines
3.9 KiB
JavaScript
|
|
/* 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-v2';
|
|||
|
|
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 staleWhileRevalidate(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 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(staleWhileRevalidate(event, request));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (url.pathname === `${BASE}/manifest.webmanifest` || url.pathname === offlineUrl) {
|
|||
|
|
event.respondWith(cacheFirst(event, request));
|
|||
|
|
}
|
|||
|
|
});
|