223 lines
6 KiB
JavaScript
223 lines
6 KiB
JavaScript
/*
|
|
* Service Worker für die Motorrad-IG.
|
|
*
|
|
* Strategie:
|
|
* - Navigationen: immer aus dem Netz (dynamische, geschützte Inhalte).
|
|
* Nur wenn das Netz nicht erreichbar ist, wird die Offline-Seite gezeigt.
|
|
* - Statische Dateien (CSS, JS, Schriften, öffentliche Bilder): erst aus
|
|
* dem Cache, im Hintergrund aktualisieren (stale-while-revalidate).
|
|
* - Geschützte Bilder unter /img_storage/ werden nie gecacht.
|
|
*/
|
|
'use strict';
|
|
|
|
var CACHE_VERSION = 'motorradig-static-v5';
|
|
var OFFLINE_URL = '/offline.html';
|
|
|
|
var PRECACHE_URLS = [
|
|
OFFLINE_URL,
|
|
'/manifest.webmanifest',
|
|
'/badge-96x96.png',
|
|
'/pwa-192x192.png',
|
|
'/pwa-512x512.png',
|
|
'/pwa-maskable-512x512.png',
|
|
'/favicon-192x192.png'
|
|
];
|
|
|
|
var STATIC_EXTENSIONS = {
|
|
'.css': true,
|
|
'.js': true,
|
|
'.mjs': true,
|
|
'.json': true,
|
|
'.webmanifest': true,
|
|
'.png': true,
|
|
'.jpg': true,
|
|
'.jpeg': true,
|
|
'.gif': true,
|
|
'.webp': true,
|
|
'.svg': true,
|
|
'.ico': true,
|
|
'.woff': true,
|
|
'.woff2': true,
|
|
'.ttf': true,
|
|
'.eot': true,
|
|
'.map': true
|
|
};
|
|
|
|
self.addEventListener('install', function (event) {
|
|
event.waitUntil((async function () {
|
|
var cache = await caches.open(CACHE_VERSION);
|
|
|
|
// Einzeln cachen, damit eine fehlende Datei die Installation nicht verhindert.
|
|
await Promise.allSettled(PRECACHE_URLS.map(function (url) {
|
|
return cache.add(new Request(url, { cache: 'reload' }));
|
|
}));
|
|
|
|
await self.skipWaiting();
|
|
})());
|
|
});
|
|
|
|
self.addEventListener('activate', function (event) {
|
|
event.waitUntil((async function () {
|
|
var keys = await caches.keys();
|
|
|
|
await Promise.all(keys.map(function (key) {
|
|
return key === CACHE_VERSION ? null : caches.delete(key);
|
|
}));
|
|
|
|
await self.clients.claim();
|
|
})());
|
|
});
|
|
|
|
/** true, wenn die Datei gefahrlos im Cache liegen darf. */
|
|
function isCacheableStatic(url) {
|
|
if (url.origin !== self.location.origin) {
|
|
return false;
|
|
}
|
|
|
|
// Geschützte Dateien (Galerien, Profilbilder, Anträge) nie cachen.
|
|
if (url.pathname.indexOf('/img_storage/') === 0) {
|
|
return false;
|
|
}
|
|
|
|
if (url.pathname === '/sw.js') {
|
|
return false;
|
|
}
|
|
|
|
var dot = url.pathname.lastIndexOf('.');
|
|
if (dot === -1) {
|
|
return false;
|
|
}
|
|
|
|
return STATIC_EXTENSIONS[url.pathname.slice(dot).toLowerCase()] === true;
|
|
}
|
|
|
|
async function networkFirstNavigation(request) {
|
|
try {
|
|
var response = await fetch(request);
|
|
|
|
if (response && response.status < 500) {
|
|
return response;
|
|
}
|
|
} catch (error) {
|
|
// Netz nicht erreichbar: unten Offline-Seite ausliefern.
|
|
}
|
|
|
|
var cache = await caches.open(CACHE_VERSION);
|
|
var offline = await cache.match(OFFLINE_URL);
|
|
|
|
return offline || new Response('Offline', {
|
|
status: 503,
|
|
headers: { 'Content-Type': 'text/plain; charset=utf-8' }
|
|
});
|
|
}
|
|
|
|
function staleWhileRevalidate(request, event) {
|
|
var network = fetch(request).then(function (response) {
|
|
if (response && response.ok && response.type === 'basic') {
|
|
var copy = response.clone();
|
|
|
|
return caches.open(CACHE_VERSION).then(function (cache) {
|
|
return cache.put(request, copy);
|
|
}).then(function () {
|
|
return response;
|
|
});
|
|
}
|
|
|
|
return response;
|
|
}).catch(function () {
|
|
return null;
|
|
});
|
|
|
|
// Aktualisierung auch dann abschließen, wenn bereits aus dem Cache geantwortet wurde.
|
|
event.waitUntil(network);
|
|
|
|
return caches.open(CACHE_VERSION).then(function (cache) {
|
|
return cache.match(request).then(function (cached) {
|
|
if (cached) {
|
|
return cached;
|
|
}
|
|
|
|
return network.then(function (response) {
|
|
return response || Response.error();
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
self.addEventListener('fetch', function (event) {
|
|
var request = event.request;
|
|
|
|
// Nur lesende Anfragen behandeln; Formulare (POST) laufen unverändert weiter.
|
|
if (request.method !== 'GET') {
|
|
return;
|
|
}
|
|
|
|
var url = new URL(request.url);
|
|
|
|
if (url.origin !== self.location.origin) {
|
|
return;
|
|
}
|
|
|
|
if (request.mode === 'navigate') {
|
|
event.respondWith(networkFirstNavigation(request));
|
|
return;
|
|
}
|
|
|
|
if (isCacheableStatic(url)) {
|
|
event.respondWith(staleWhileRevalidate(request, event));
|
|
}
|
|
});
|
|
|
|
// Benachrichtigungen per Web-Push (VAPID). Die Nutzlast kommt als JSON
|
|
// (Titel, Text, URL, optional Tag) vom Server.
|
|
self.addEventListener('push', function (event) {
|
|
var data = {};
|
|
|
|
try {
|
|
data = event.data ? event.data.json() : {};
|
|
} catch (error) {
|
|
data = {};
|
|
}
|
|
|
|
var title = data.title || 'Motorrad-IG';
|
|
var options = {
|
|
body: data.body || '',
|
|
icon: '/pwa-192x192.png',
|
|
// Statusleisten-Icon (Android): muss eine einfarbige Silhouette mit
|
|
// transparentem Hintergrund sein. Ein farbiges/opakes Icon wird von
|
|
// Android als weißes Quadrat dargestellt.
|
|
badge: '/badge-96x96.png',
|
|
tag: data.tag || undefined,
|
|
data: { url: data.url || '/' }
|
|
};
|
|
|
|
event.waitUntil(self.registration.showNotification(title, options));
|
|
});
|
|
|
|
self.addEventListener('notificationclick', function (event) {
|
|
event.notification.close();
|
|
|
|
var target = (event.notification.data && event.notification.data.url) || '/';
|
|
|
|
event.waitUntil(
|
|
clients.matchAll({ type: 'window', includeUncontrolled: true }).then(function (list) {
|
|
for (var i = 0; i < list.length; i++) {
|
|
var client = list[i];
|
|
|
|
if (new URL(client.url).origin !== self.location.origin) {
|
|
continue;
|
|
}
|
|
|
|
return client.focus().then(function () {
|
|
if ('navigate' in client) {
|
|
return client.navigate(target);
|
|
}
|
|
|
|
return client;
|
|
});
|
|
}
|
|
|
|
return clients.openWindow(target);
|
|
})
|
|
);
|
|
});
|