diff --git a/motorradig/README.md b/motorradig/README.md index 0f1298e..dc4a555 100644 --- a/motorradig/README.md +++ b/motorradig/README.md @@ -13,6 +13,20 @@ Keine externen Composer-Abhängigkeiten. - Login/Logout, Registrierung, Passwort-Reset per E-Mail - PDF-Upload (Tourenplan, Mitgliedsantrag) - Bilderspeicher unter `storage/img_storage/` (geschützt ausgeliefert) +- Installierbar als PWA (Manifest, Service Worker, Offline-Seite) + +## PWA + +Die Website ist als installierbare Progressive Web App eingerichtet: + +- `public/manifest.webmanifest` – Name, Icons, Startseite und Shortcuts +- `public/sw.js` – Service Worker: statische Dateien aus dem Cache, + Navigationen aus dem Netz, Offline-Fallback +- `public/offline.html` – angezeigte Seite ohne Internetverbindung + +Geschützte Dateien unter `/img_storage/` werden bewusst nicht gecacht. +Bei Änderungen an den vorgeladenen Dateien `CACHE_VERSION` in `public/sw.js` +erhöhen, damit alte Caches ersetzt werden. ## Struktur diff --git a/motorradig/public/.htaccess b/motorradig/public/.htaccess index 3aec5e2..3f26b6a 100644 --- a/motorradig/public/.htaccess +++ b/motorradig/public/.htaccess @@ -1,3 +1,18 @@ +# PWA: Manifest mit korrektem MIME-Typ ausliefern und Service Worker +# nicht dauerhaft im Browser-Cache halten. + + AddType application/manifest+json .webmanifest + + + + + Header set Cache-Control "no-cache, no-store, must-revalidate" + + + Header set Cache-Control "no-cache" + + + Options -MultiViews -Indexes diff --git a/motorradig/public/manifest.webmanifest b/motorradig/public/manifest.webmanifest new file mode 100644 index 0000000..041cfca --- /dev/null +++ b/motorradig/public/manifest.webmanifest @@ -0,0 +1,77 @@ +{ + "id": "/", + "name": "Motorrad IG", + "short_name": "Motorrad IG", + "description": "Touren, Aktivitäten, Mitglieder und Galerien der Motorrad-IG.", + "lang": "de", + "dir": "ltr", + "start_url": "/", + "scope": "/", + "display": "standalone", + "display_override": ["standalone", "minimal-ui"], + "orientation": "any", + "background_color": "#0d1117", + "theme_color": "#343a40", + "categories": ["sports", "lifestyle", "travel"], + "icons": [ + { + "src": "/pwa-192x192.png", + "sizes": "192x192", + "type": "image/png", + "purpose": "any" + }, + { + "src": "/pwa-512x512.png", + "sizes": "512x512", + "type": "image/png", + "purpose": "any" + }, + { + "src": "/pwa-maskable-512x512.png", + "sizes": "512x512", + "type": "image/png", + "purpose": "maskable" + } + ], + "shortcuts": [ + { + "name": "Aktivitäten", + "short_name": "Aktivitäten", + "description": "Termine und Aktivitäten ansehen", + "url": "/activities", + "icons": [ + { + "src": "/pwa-192x192.png", + "sizes": "192x192", + "type": "image/png" + } + ] + }, + { + "name": "Tourenplan", + "short_name": "Touren", + "description": "Touren und Anmeldungen ansehen", + "url": "/tour", + "icons": [ + { + "src": "/pwa-192x192.png", + "sizes": "192x192", + "type": "image/png" + } + ] + }, + { + "name": "Mitgliedsantrag", + "short_name": "Antrag", + "description": "Mitgliedsantrag ausfüllen", + "url": "/mitgliedsantrag", + "icons": [ + { + "src": "/pwa-192x192.png", + "sizes": "192x192", + "type": "image/png" + } + ] + } + ] +} diff --git a/motorradig/public/offline.html b/motorradig/public/offline.html new file mode 100644 index 0000000..e6419d3 --- /dev/null +++ b/motorradig/public/offline.html @@ -0,0 +1,97 @@ + + + + + + + + Keine Verbindung – Motorrad IG + + + +
+ +

Keine Verbindung

+

+ Die Seite konnte nicht geladen werden. Prüfe deine Internetverbindung + und versuche es erneut. +

+
+ + Zur Startseite +
+
+ + diff --git a/motorradig/public/pwa-192x192.png b/motorradig/public/pwa-192x192.png new file mode 100644 index 0000000..1f16233 Binary files /dev/null and b/motorradig/public/pwa-192x192.png differ diff --git a/motorradig/public/pwa-512x512.png b/motorradig/public/pwa-512x512.png new file mode 100644 index 0000000..810a296 Binary files /dev/null and b/motorradig/public/pwa-512x512.png differ diff --git a/motorradig/public/pwa-maskable-512x512.png b/motorradig/public/pwa-maskable-512x512.png new file mode 100644 index 0000000..72d0728 Binary files /dev/null and b/motorradig/public/pwa-maskable-512x512.png differ diff --git a/motorradig/public/sw.js b/motorradig/public/sw.js new file mode 100644 index 0000000..4a954cd --- /dev/null +++ b/motorradig/public/sw.js @@ -0,0 +1,164 @@ +/* + * 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-v1'; +var OFFLINE_URL = '/offline.html'; + +var PRECACHE_URLS = [ + OFFLINE_URL, + '/manifest.webmanifest', + '/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') { + caches.open(CACHE_VERSION).then(function (cache) { + cache.put(request, response.clone()); + }); + } + + 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)); + } +}); diff --git a/motorradig/views/layout.php b/motorradig/views/layout.php index 942a73f..3ce8334 100644 --- a/motorradig/views/layout.php +++ b/motorradig/views/layout.php @@ -23,6 +23,15 @@ + + + + + + + + + @@ -69,6 +78,10 @@