141 lines
3.6 KiB
JavaScript
141 lines
3.6 KiB
JavaScript
// /sw.js
|
|
const CACHE = 'uslugi-v8';
|
|
const ASSETS = [
|
|
'/',
|
|
'/index.php',
|
|
'/offline.html',
|
|
'/public/uploads/img/icon-192.png',
|
|
'/public/uploads/img/icon-512.png',
|
|
];
|
|
|
|
const BYPASS = [
|
|
/^\/public\/contact_admin\.php$/,
|
|
/^\/auth\//,
|
|
/^\/messages\/api\//,
|
|
/^\/push\//,
|
|
/^\/badge_count\.php$/,
|
|
];
|
|
|
|
self.addEventListener('install', (e) => {
|
|
e.waitUntil(caches.open(CACHE).then((c) => c.addAll(ASSETS)).catch(() => {}));
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', (e) => {
|
|
e.waitUntil((async () => {
|
|
if ('navigationPreload' in self.registration) {
|
|
await self.registration.navigationPreload.enable();
|
|
}
|
|
const keys = await caches.keys();
|
|
await Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)));
|
|
})());
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener('fetch', (e) => {
|
|
const req = e.request;
|
|
const url = new URL(req.url);
|
|
|
|
if (BYPASS.some((rx) => rx.test(url.pathname))) return;
|
|
|
|
if (req.mode === 'navigate') {
|
|
e.respondWith((async () => {
|
|
try {
|
|
const preload = await e.preloadResponse;
|
|
if (preload) return preload;
|
|
|
|
const network = await fetch(req);
|
|
if (network && network.ok) {
|
|
const copy = network.clone();
|
|
caches.open(CACHE).then((c) => c.put(req, copy));
|
|
}
|
|
return network;
|
|
} catch {
|
|
const cached = await caches.match(req);
|
|
return cached || caches.match('/offline.html');
|
|
}
|
|
})());
|
|
return;
|
|
}
|
|
|
|
if (req.method !== 'GET') return;
|
|
|
|
e.respondWith(
|
|
caches.match(req).then((cached) => {
|
|
const network = fetch(req)
|
|
.then((res) => {
|
|
if (res && res.status === 200) {
|
|
const copy = res.clone();
|
|
caches.open(CACHE).then((c) => c.put(req, copy));
|
|
}
|
|
return res;
|
|
})
|
|
.catch(() => cached);
|
|
return cached || network;
|
|
})
|
|
);
|
|
});
|
|
|
|
self.addEventListener('push', (event) => {
|
|
let data = {};
|
|
try { data = event.data ? event.data.json() : {}; } catch (e) {}
|
|
|
|
const title = data.title || 'USLUGI.FR';
|
|
const body = data.body || '';
|
|
const url = data.url || '/';
|
|
const badgeCount = Number(data.badgeCount ?? 0) || 0;
|
|
|
|
const options = {
|
|
body,
|
|
data: { ...data, url },
|
|
icon: '/public/uploads/img/icon-192.png',
|
|
badge: '/public/uploads/img/icon-192.png',
|
|
};
|
|
|
|
event.waitUntil((async () => {
|
|
await self.registration.showNotification(title, options);
|
|
|
|
// iOS PWA badge
|
|
if ('setAppBadge' in self.navigator) {
|
|
try {
|
|
if (badgeCount > 0) await self.navigator.setAppBadge(badgeCount);
|
|
else if ('clearAppBadge' in self.navigator) await self.navigator.clearAppBadge();
|
|
} catch (_) {}
|
|
}
|
|
|
|
// informer les pages ouvertes (optionnel)
|
|
const cl = await clients.matchAll({ type: 'window', includeUncontrolled: true });
|
|
for (const c of cl) {
|
|
c.postMessage({ type: 'SET_BADGE', badgeCount });
|
|
}
|
|
})());
|
|
});
|
|
|
|
self.addEventListener('notificationclick', event => {
|
|
event.notification.close();
|
|
|
|
const data = event.notification.data || {};
|
|
const targetUrl = data.url || '/';
|
|
|
|
event.waitUntil(
|
|
(async () => {
|
|
const allClients = await clients.matchAll({
|
|
type: 'window',
|
|
includeUncontrolled: true
|
|
});
|
|
|
|
// 🔁 Si une fenêtre est déjà ouverte → focus + navigation
|
|
for (const client of allClients) {
|
|
if ('navigate' in client) {
|
|
await client.navigate(targetUrl);
|
|
return client.focus();
|
|
}
|
|
}
|
|
|
|
// 🆕 Sinon → nouvelle fenêtre
|
|
return clients.openWindow(targetUrl);
|
|
})()
|
|
);
|
|
});
|
|
|