-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
105 lines (104 loc) · 3.55 KB
/
serviceworker.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var staticCacheName = "pwa";
self.addEventListener("install", function (event) {
event.waitUntil(preLoad());
});
self.addEventListener("fetch", function (event) {
event.respondWith(
checkResponse(event.request).catch(function () {
console.log("Fetch from cache successful!");
return returnFromCache(event.request);
})
);
console.log("Fetch successful!");
event.waitUntil(addToCache(event.request));
});
self.addEventListener("sync", (event) => {
if (event.tag === "syncMessage") {
console.log("Sync successful!");
}
});
self.addEventListener("push", function (event) {
if (event && event.data) {
var data = event.data.json();
if (data.method == "pushMessage") {
console.log("Push notification requested");
if (Notification.permission === "granted") {
event.waitUntil(
self.registration.showNotification("To Do List", {
body: data.message,
}).catch(function(error) {
console.error("Error showing notification:", error);
})
);
} else if (Notification.permission === "denied") {
console.error("Notification permission denied.");
// Handle the case where notification permission is denied, such as showing a message to
//the user.
} else {
Notification.requestPermission().then(function(permission) {
if (permission === "granted") {
console.log("Notification permission granted, showing notification");
self.registration.showNotification("To Do List", {
body: data.message,
});
} else {
console.error("Notification permission denied.");
// Handle the case where notification permission is denied after requesting.
}
});
}
}
}
});
var filesToCache = ["/", "/index.html"];
var preLoad = function () {
return caches.open("index").then(function (cache) {
return cache.addAll(filesToCache);
});
};
var checkResponse = function (request) {
return new Promise(function (fulfill, reject) {
fetch(request)
.then(function (response) {
if (response.status !== 404) {
fulfill(response);
} else {
reject();
}
})
.catch(reject);
});
};
var addToCache = function (request) {
return caches.open("index").then(function (cache) {
return fetch(request).then(function (response) {
return cache.put(request, response);
});
});
};
var returnFromCache = function (request) {
return caches.open("index").then(function (cache) {
return cache.match(request).then(function (matching) {
if (!matching || matching.status == 404) {
return cache.match("index.html");
} else {
return matching;
}
});
});
};
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => {
return name !== staticCacheName;
})
.map((name) => {
return caches.delete(name);
})
);
})
);
});