-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
76 lines (57 loc) · 1.52 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
const CACHE_NAME = "kiss-app-static-files";
const FILES_STATIC = [
'css/bootstrap.min.css',
'css/styles.css',
'icons/favicon.ico',
'icons/152.png',
'imgs/alicecooper.jpg',
'imgs/bg-footer.png',
'imgs/cat_icon.png',
'imgs/engenheiros-do-hawaii.jpg',
'imgs/icon.png',
'imgs/joan-jett.jpg',
'imgs/loading.gif',
'imgs/logo1.png',
"imgs/megadeth.jpg",
'imgs/offline.png',
'js/app.js',
'js/bootstrap.bundle.min.js',
'offline.html'
];
//Instalação
self.addEventListener('install', (evt) => {
evt.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log("Service Worker realizando caches estáticos");
return cache.addAll(FILES_STATIC);
})
);
self.skipWaiting();
});
//Ativação
self.addEventListener('activate', (evt) => {
evt.waitUntil(
caches.keys().then((keylist) => {
return Promise.all(keylist.map((key) => {
if(key !== CACHE_NAME){
return caches.delete(key);
}
}));
})
);
self.clients.claim();
});
//Responder um página Offline (fetch)
self.addEventListener('fetch', (evt) => {
if(evt.request.mode !== 'navigate'){
return;
}
evt.respondWith(
fetch(evt.request).catch(() => {
return caches.open(CACHE_NAME).then((cache) => {
return cache.match('offline.html');
})
})
);
});