74 lines
2.2 KiB
JavaScript
74 lines
2.2 KiB
JavaScript
/**
|
||
* DeskHub Service Worker – Mitarbeiterbereich (scope: /staff/)
|
||
*/
|
||
const CACHE = 'deskhub-staff-v2';
|
||
const OFFLINE_URL = '/offline.html';
|
||
const PRECACHE = ['/offline.html', '/assets/css/style.css'];
|
||
|
||
self.addEventListener('install', e => {
|
||
e.waitUntil(caches.open(CACHE).then(c => c.addAll(PRECACHE)));
|
||
self.skipWaiting();
|
||
});
|
||
|
||
self.addEventListener('activate', e => {
|
||
e.waitUntil(
|
||
caches.keys().then(keys =>
|
||
Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))
|
||
)
|
||
);
|
||
self.clients.claim();
|
||
});
|
||
|
||
self.addEventListener('fetch', e => {
|
||
if (e.request.method !== 'GET') return;
|
||
|
||
if (e.request.url.match(/\.(css|js|woff2?|png|svg|ico|webp)(\?.*)?$/)) {
|
||
e.respondWith(
|
||
caches.match(e.request).then(cached =>
|
||
cached || fetch(e.request).then(resp => {
|
||
caches.open(CACHE).then(c => c.put(e.request, resp.clone()));
|
||
return resp;
|
||
})
|
||
)
|
||
);
|
||
return;
|
||
}
|
||
|
||
e.respondWith(
|
||
fetch(e.request).catch(() => caches.match(OFFLINE_URL))
|
||
);
|
||
});
|
||
|
||
// ── Push Notifications ────────────────────────────────────────────────────────
|
||
self.addEventListener('push', e => {
|
||
let data = { title: 'DeskHub', body: 'Neue Benachrichtigung', url: '/staff/', icon: '/assets/icons/icon-192.png' };
|
||
try { data = Object.assign(data, e.data.json()); } catch {}
|
||
e.waitUntil(
|
||
self.registration.showNotification(data.title, {
|
||
body: data.body,
|
||
icon: data.icon,
|
||
badge: data.badge || '/assets/icons/icon-192.png',
|
||
data: { url: data.url },
|
||
vibrate: [150, 50, 150],
|
||
tag: 'deskhub-' + (data.url || ''),
|
||
renotify: true,
|
||
})
|
||
);
|
||
});
|
||
|
||
self.addEventListener('notificationclick', e => {
|
||
e.notification.close();
|
||
const url = e.notification.data?.url || '/staff/';
|
||
e.waitUntil(
|
||
clients.matchAll({ type: 'window', includeUncontrolled: true }).then(list => {
|
||
for (const c of list) {
|
||
if (c.url.includes('/staff/') && 'focus' in c) {
|
||
c.navigate(url);
|
||
return c.focus();
|
||
}
|
||
}
|
||
return clients.openWindow(url);
|
||
})
|
||
);
|
||
});
|