Website als PWA installierbar machen
This commit is contained in:
parent
53ab930ddf
commit
90e83d6367
9 changed files with 435 additions and 0 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,18 @@
|
|||
# PWA: Manifest mit korrektem MIME-Typ ausliefern und Service Worker
|
||||
# nicht dauerhaft im Browser-Cache halten.
|
||||
<IfModule mod_mime.c>
|
||||
AddType application/manifest+json .webmanifest
|
||||
</IfModule>
|
||||
|
||||
<IfModule mod_headers.c>
|
||||
<Files "sw.js">
|
||||
Header set Cache-Control "no-cache, no-store, must-revalidate"
|
||||
</Files>
|
||||
<Files "manifest.webmanifest">
|
||||
Header set Cache-Control "no-cache"
|
||||
</Files>
|
||||
</IfModule>
|
||||
|
||||
<IfModule mod_rewrite.c>
|
||||
<IfModule mod_negotiation.c>
|
||||
Options -MultiViews -Indexes
|
||||
|
|
|
|||
77
motorradig/public/manifest.webmanifest
Normal file
77
motorradig/public/manifest.webmanifest
Normal file
|
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
97
motorradig/public/offline.html
Normal file
97
motorradig/public/offline.html
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="theme-color" content="#343a40">
|
||||
<meta name="robots" content="noindex">
|
||||
<title>Keine Verbindung – Motorrad IG</title>
|
||||
<style>
|
||||
:root { color-scheme: dark; }
|
||||
* { box-sizing: border-box; }
|
||||
html, body { height: 100%; }
|
||||
body {
|
||||
margin: 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 24px;
|
||||
font-family: Nunito, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
||||
color: #e8edf5;
|
||||
background:
|
||||
radial-gradient(900px 480px at 20% -10%, rgba(78, 161, 255, .18), transparent 60%),
|
||||
#0d1117;
|
||||
}
|
||||
.card {
|
||||
width: 100%;
|
||||
max-width: 460px;
|
||||
padding: 36px 30px;
|
||||
text-align: center;
|
||||
background: rgba(255, 255, 255, .05);
|
||||
border: 1px solid rgba(255, 255, 255, .1);
|
||||
border-radius: 22px;
|
||||
box-shadow: 0 24px 60px rgba(0, 0, 0, .45);
|
||||
}
|
||||
.logo {
|
||||
width: 96px;
|
||||
height: 96px;
|
||||
margin-bottom: 18px;
|
||||
border-radius: 50%;
|
||||
background: #fff;
|
||||
}
|
||||
h1 {
|
||||
margin: 0 0 12px;
|
||||
font-size: 1.7rem;
|
||||
line-height: 1.2;
|
||||
}
|
||||
p {
|
||||
margin: 0 0 26px;
|
||||
color: #b9c6d8;
|
||||
line-height: 1.6;
|
||||
}
|
||||
.actions {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 10px;
|
||||
justify-content: center;
|
||||
}
|
||||
a, button {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: .65rem 1.2rem;
|
||||
font: inherit;
|
||||
font-weight: 700;
|
||||
text-decoration: none;
|
||||
border-radius: 999px;
|
||||
cursor: pointer;
|
||||
}
|
||||
button {
|
||||
color: #fff;
|
||||
background: #e5484d;
|
||||
border: 1px solid #e5484d;
|
||||
}
|
||||
a {
|
||||
color: #e8edf5;
|
||||
background: transparent;
|
||||
border: 1px solid rgba(255, 255, 255, .3);
|
||||
}
|
||||
button:hover, button:focus { background: #cf3b40; }
|
||||
a:hover, a:focus { border-color: rgba(255, 255, 255, .6); }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main class="card">
|
||||
<img class="logo" src="/pwa-192x192.png" alt="Motorrad IG" width="96" height="96">
|
||||
<h1>Keine Verbindung</h1>
|
||||
<p>
|
||||
Die Seite konnte nicht geladen werden. Prüfe deine Internetverbindung
|
||||
und versuche es erneut.
|
||||
</p>
|
||||
<div class="actions">
|
||||
<button type="button" onclick="location.reload()">Erneut versuchen</button>
|
||||
<a href="/">Zur Startseite</a>
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
BIN
motorradig/public/pwa-192x192.png
Normal file
BIN
motorradig/public/pwa-192x192.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
BIN
motorradig/public/pwa-512x512.png
Normal file
BIN
motorradig/public/pwa-512x512.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 98 KiB |
BIN
motorradig/public/pwa-maskable-512x512.png
Normal file
BIN
motorradig/public/pwa-maskable-512x512.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 78 KiB |
164
motorradig/public/sw.js
Normal file
164
motorradig/public/sw.js
Normal file
|
|
@ -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));
|
||||
}
|
||||
});
|
||||
|
|
@ -23,6 +23,15 @@
|
|||
<link rel="icon" type="image/png" sizes="192x192" href="<?= e(asset_versioned('favicon-192x192.png')) ?>">
|
||||
<link rel="apple-touch-icon" sizes="180x180" href="<?= e(asset('apple-touch-icon.png')) ?>">
|
||||
|
||||
<!-- PWA -->
|
||||
<link rel="manifest" href="<?= e(asset_versioned('manifest.webmanifest')) ?>">
|
||||
<meta name="theme-color" content="#343a40">
|
||||
<meta name="application-name" content="<?= e(config('app.name')) ?>">
|
||||
<meta name="mobile-web-app-capable" content="yes">
|
||||
<meta name="apple-mobile-web-app-capable" content="yes">
|
||||
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
|
||||
<meta name="apple-mobile-web-app-title" content="<?= e(config('app.name')) ?>">
|
||||
|
||||
<!-- Scripts -->
|
||||
<script src="<?= e(asset_versioned('js/app.js')) ?>" defer></script>
|
||||
|
||||
|
|
@ -69,6 +78,10 @@
|
|||
|
||||
<!-- Rechte Seite der Navigation -->
|
||||
<ul class="navbar-nav ml-auto">
|
||||
<li class="nav-item">
|
||||
<button type="button" class="nav-link" id="pwa-install-button" hidden
|
||||
style="background:none;border:0;cursor:pointer;font:inherit;">App installieren</button>
|
||||
</li>
|
||||
<?php if (!$user): ?>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/login">Login</a>
|
||||
|
|
@ -127,5 +140,60 @@
|
|||
<?= $content ?? '' ?>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
if ('serviceWorker' in navigator) {
|
||||
window.addEventListener('load', function () {
|
||||
navigator.serviceWorker.register('/sw.js', {
|
||||
scope: '/',
|
||||
updateViaCache: 'none'
|
||||
}).catch(function () {
|
||||
// Ohne Service Worker funktioniert die Website weiterhin normal.
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
var installButton = document.getElementById('pwa-install-button');
|
||||
var deferredPrompt = null;
|
||||
|
||||
if (!installButton) {
|
||||
return;
|
||||
}
|
||||
|
||||
window.addEventListener('beforeinstallprompt', function (event) {
|
||||
event.preventDefault();
|
||||
deferredPrompt = event;
|
||||
installButton.hidden = false;
|
||||
});
|
||||
|
||||
installButton.addEventListener('click', function () {
|
||||
if (!deferredPrompt) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
deferredPrompt.prompt();
|
||||
} catch (error) {
|
||||
deferredPrompt = null;
|
||||
installButton.hidden = true;
|
||||
return;
|
||||
}
|
||||
|
||||
deferredPrompt.userChoice.then(function () {
|
||||
deferredPrompt = null;
|
||||
installButton.hidden = true;
|
||||
}).catch(function () {
|
||||
deferredPrompt = null;
|
||||
installButton.hidden = true;
|
||||
});
|
||||
});
|
||||
|
||||
window.addEventListener('appinstalled', function () {
|
||||
deferredPrompt = null;
|
||||
installButton.hidden = true;
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue