-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
111 lines (49 loc) · 1.76 KB
/
service-worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const paginaOffline = "offline.html";
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open('Offline').then(function(cache) {
return cache.addAll([
'favicon.ico',
'res/192x192.png',
'res/bg_2_1920x1080.png',
'style/allstyles.css',
'style/home.css',
'manifest.json',
paginaOffline
]);
})
);
});
self.addEventListener('fetch', (event) => {
// Rispondiamo solo a una richiesta per una pagina web
if (event.request.mode === 'navigate') {
event.respondWith((async () => {
try {
// Prova a usare la preloadResponse
const preloadResponse = await event.preloadResponse;
if (preloadResponse) {
return preloadResponse;
}
const networkResponse = await fetch(event.request);
return networkResponse;
} catch (error) {
// Qualcosa è andato storto; probabilmente sei offline!
console.log('Restituisco la pagina offline', error);
const cache = await caches.open('Offline');
const cachedResponse = await cache.match(paginaOffline);
return cachedResponse;
}
})());
}
});
self.addEventListener('push', function(event) {
console.log('[Service Worker] Push Received.');
console.log(`[Service Worker] Push had this data: "${event.data.text()}"`);
const title = 'Appunti';
const options = {
body: 'Sei offline!',
icon: 'res/favicon.ico',
badge: 'res/logo_iconato.png'
};
event.waitUntil(self.registration.showNotification(title, options));
});