PWA: Manifest, Service Worker, Icons und Offline-Seite
This commit is contained in:
parent
ec88c8ebac
commit
67a1614081
19 changed files with 472 additions and 5 deletions
|
|
@ -3,7 +3,48 @@
|
|||
'use strict';
|
||||
|
||||
const csrf = document.querySelector('meta[name="csrf-token"]')?.content || '';
|
||||
const swMeta = document.querySelector('meta[name="sw-url"]');
|
||||
let toastTimer = 0;
|
||||
let installPrompt = null;
|
||||
|
||||
/* --------------------------------------------------------------- PWA */
|
||||
const registerServiceWorker = () => {
|
||||
if (!('serviceWorker' in navigator) || !swMeta) return;
|
||||
navigator.serviceWorker.register(swMeta.content).catch(() => {
|
||||
/* Installation ist optional; die App funktioniert auch ohne SW. */
|
||||
});
|
||||
};
|
||||
if (document.readyState === 'complete') {
|
||||
registerServiceWorker();
|
||||
} else {
|
||||
window.addEventListener('load', registerServiceWorker, { once: true });
|
||||
}
|
||||
|
||||
window.addEventListener('beforeinstallprompt', (event) => {
|
||||
event.preventDefault();
|
||||
installPrompt = event;
|
||||
document.querySelectorAll('[data-install-app]').forEach((button) => { button.hidden = false; });
|
||||
});
|
||||
|
||||
window.addEventListener('appinstalled', () => {
|
||||
installPrompt = null;
|
||||
document.querySelectorAll('[data-install-app]').forEach((button) => { button.hidden = true; });
|
||||
toast('Die App wurde installiert.');
|
||||
});
|
||||
|
||||
document.addEventListener('click', async (event) => {
|
||||
const button = event.target.closest('[data-install-app]');
|
||||
if (!button || !installPrompt) return;
|
||||
const prompt = installPrompt;
|
||||
installPrompt = null;
|
||||
button.hidden = true;
|
||||
try {
|
||||
prompt.prompt();
|
||||
await prompt.userChoice;
|
||||
} catch (error) {
|
||||
/* Der Browser kann die Installation trotzdem abgelehnt haben. */
|
||||
}
|
||||
});
|
||||
|
||||
const toast = (message, isError = false) => {
|
||||
const el = document.getElementById('toast');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue