-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
74 lines (69 loc) · 2.04 KB
/
sw.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
const CACHE_NAME = "my-site-cache-v1";
const urlsToCache = [
"./",
"index.html",
"css/bootstrap.min.css",
"css/main.min.css",
];
self.addEventListener("install", function (event) {
event.waitUntil(
/* Este método estende o evento ONINSTALL e aplica um estado ao evento chamado ONINSTALLING */
caches
.open(
CACHE_NAME
) /* O objeto caches é criado com um namespace e retorna uma Promise */
.then(function (cache) {
// console.log('Cache aberto');
return cache.addAll(
urlsToCache
); /* E por fim, conseguimos manipular o objeto de cache corrente */
})
);
});
self.addEventListener("activate", function (event) {
console.log("activate");
event.waitUntil(
cache.keys().then(function (keys) {
return Promise.all(
keys
.filter(function (key) {
return key.indexOf(CACHE_NAME) !== 0;
})
.map(function (key) {
return caches.delete(key);
})
);
})
);
});
self.addEventListener("fetch", function (event) {
event.respondWith(
caches.match(event.request).then(function (response) {
return response || fetch(event.request);
})
);
});
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("sw.js")
.then(function (registration) {
// Registro realizado com sucesso (NOTA: Observe que declaro um arquivo chamado sw.js, ele é onde colocaremos as notações do nosso Service Workers)
console.log(
"O ServiceWorker foi registrado com escopo: ",
registration.scope
);
})
.catch(function (err) {
// O registro falhou :(
console.log("O registro do ServiceWorker falhou com o erro: ", err);
});
}
if (window.Notification && Notification.permission !== "denied") {
Notification.requestPermission(function (status) {
// status is "granted", if accepted by user
var n = new Notification("Prof manu", {
body: "Bem-vindo à ao meu site",
icon: "img/icons/icon-72x72.png", // optional
});
});
}