All checks were successful
Deploy baustelle-pwa / deploy (push) Successful in 1s
Chrome Android zeigte nur 'Zum Startbildschirm hinzufügen' statt 'App installieren', weil: 1. 'any maskable' als kombinierter purpose reicht Chrome nicht — braucht mindestens ein reines 'any'-Icon für den Install-Prompt 2. start_url/scope waren relativ (./) — mit absoluten Pfaden zum custom/baustelle/-Root ist der Scope eindeutig Fixes: - icons: je 192/512 als 'any' UND zusätzlich als 'maskable' (4 Einträge) - id, start_url, scope auf '/custom/baustelle/' gesetzt (absolut) - categories, dir: ltr dazu für vollständiges Manifest Damit erfüllt die PWA die Chrome-Installability-Kriterien und der Menü-Eintrag wird zu 'App installieren'. Erst dann funktioniert auch Web Share Target. Cache v10. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> [deploy]
122 lines
3.8 KiB
JavaScript
122 lines
3.8 KiB
JavaScript
/* Baustelle PWA Service Worker — v9
|
|
* Pattern nach claude-db #31:
|
|
* - Network-First für eigene Assets (immer aktuell, Fallback Cache)
|
|
* - skipWaiting() + clients.claim() damit Updates sofort greifen
|
|
* - Web Share Target via POST → share.html
|
|
*/
|
|
|
|
const CACHE = 'baustelle-v10';
|
|
const SHELL = [
|
|
'./',
|
|
'./index.html',
|
|
'./share.html',
|
|
'./app.css',
|
|
'./app.js',
|
|
'./manifest.webmanifest',
|
|
'./lib/idb.js',
|
|
'./lib/api.js',
|
|
'./lib/offline.js',
|
|
'./lib/router.js',
|
|
'./icons/icon-192.png',
|
|
'./icons/icon-512.png',
|
|
'./icons/icon.svg',
|
|
];
|
|
|
|
self.addEventListener('install', (e) => {
|
|
e.waitUntil(
|
|
caches.open(CACHE)
|
|
.then(c => c.addAll(SHELL).catch(() => null))
|
|
.then(() => self.skipWaiting())
|
|
);
|
|
});
|
|
|
|
self.addEventListener('activate', (e) => {
|
|
e.waitUntil(
|
|
caches.keys()
|
|
.then(keys => Promise.all(
|
|
keys.filter(k => k !== CACHE).map(k => caches.delete(k))
|
|
))
|
|
.then(() => self.clients.claim())
|
|
);
|
|
});
|
|
|
|
/* Web Share Target: POST auf share.html abfangen */
|
|
async function handleShareTarget(request) {
|
|
const fd = await request.formData();
|
|
let files = [];
|
|
for (const key of ['photos', 'file', 'files', 'image', 'images']) {
|
|
const v = fd.getAll(key);
|
|
if (v && v.length) files = files.concat(v);
|
|
}
|
|
// Fallback: alle File-Entries im FormData durchsuchen
|
|
if (!files.length) {
|
|
for (const [, v] of fd.entries()) {
|
|
if (v && typeof v === 'object' && v.size !== undefined) files.push(v);
|
|
}
|
|
}
|
|
|
|
if (files.length) {
|
|
const db = await new Promise((res, rej) => {
|
|
const req = indexedDB.open('baustelle-pwa-v1', 1);
|
|
req.onupgradeneeded = () => {
|
|
const d = req.result;
|
|
if (!d.objectStoreNames.contains('kv')) d.createObjectStore('kv');
|
|
if (!d.objectStoreNames.contains('queue')) d.createObjectStore('queue', { keyPath: 'id', autoIncrement: true });
|
|
};
|
|
req.onsuccess = () => res(req.result);
|
|
req.onerror = () => rej(req.error);
|
|
});
|
|
const tx = db.transaction('kv', 'readwrite');
|
|
tx.objectStore('kv').put(
|
|
files.map(f => ({ name: f.name || 'photo.jpg', type: f.type || 'image/jpeg', data: f })),
|
|
'shared_files'
|
|
);
|
|
await new Promise(res => { tx.oncomplete = res; });
|
|
}
|
|
return Response.redirect('./share.html', 303);
|
|
}
|
|
|
|
self.addEventListener('fetch', (e) => {
|
|
const url = new URL(e.request.url);
|
|
|
|
// Web Share Target POST → share.html
|
|
if (e.request.method === 'POST' && url.pathname.endsWith('/share.html')) {
|
|
e.respondWith(handleShareTarget(e.request));
|
|
return;
|
|
}
|
|
|
|
// API-Requests: nicht cachen, durchreichen
|
|
if (url.pathname.includes('/custom/bericht/api/')) {
|
|
return;
|
|
}
|
|
|
|
// Nicht-GETs: durchreichen
|
|
if (e.request.method !== 'GET') {
|
|
return;
|
|
}
|
|
|
|
// Eigene Assets: Network-First mit Cache-Fallback
|
|
if (url.origin === location.origin) {
|
|
e.respondWith(
|
|
fetch(e.request)
|
|
.then(response => {
|
|
// Cache aktualisieren (nur erfolgreiche responses)
|
|
if (response && response.ok) {
|
|
const clone = response.clone();
|
|
caches.open(CACHE).then(c => c.put(e.request, clone)).catch(() => null);
|
|
}
|
|
return response;
|
|
})
|
|
.catch(() => caches.match(e.request))
|
|
);
|
|
return;
|
|
}
|
|
});
|
|
|
|
/* Message-Handler: Client kann "SKIP_WAITING" schicken um ein wartenden SW
|
|
* sofort zu aktivieren (für In-App-Update-Button). */
|
|
self.addEventListener('message', (e) => {
|
|
if (e.data && e.data.type === 'SKIP_WAITING') {
|
|
self.skipWaiting();
|
|
}
|
|
});
|