-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
39 lines (33 loc) · 866 Bytes
/
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
var CACHE_NAME = 'pwa-sample-caches';
var urlsToCache = [
"index.html",
"icon.svg"
];
self.addEventListener('install', function(event) {
console.log('sw event: install called');
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('fetch', function(event) {
console.log('sw event: fetch called');
event.respondWith(
caches.match(event.request)
.then(function(response) {
return response ? response : fetch(event.request);
})
);
});
self.addEventListener('push', function(event){
console.log('sw event: push called');
var notificationDataObj = event.data.json();
var content = {
body: notificationDataObj.body,
};
event.waitUntil(
self.registration.showNotification(notificationDataObj.title, content)
);
});