-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
64 lines (59 loc) · 1.49 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
self.addEventListener('install', event => {
console.log('SW installed at: ', new Date().toLocaleTimeString());
event.waitUntil(
caches.open('IB_Cache').then((cache) => {
return cache.addAll(['index.html',
'css/styles.css',
'js/index.js',
'offline.html'
])
})
);
self.skipWaiting();
});
self.addEventListener('activate', event => {
console.log('SW activated at: ', new Date().toLocaleTimeString());
});
self.addEventListener('fetch', event => {
if (event.request.method === 'GET') {
event.respondWith(
caches.match(event.request)
.then((response) => {
if (!navigator.onLine) {
if (response) {
return response;
} else {
return caches.match(new Request('offline.html'));
}
} else {
return updateCache(event.request);
}
})
)
}
});
self.addEventListener('push', (event) => {
if (event.data) {
createNotification(event.data.text());
}
})
const createNotification = (text) => {
self.registration.showNotification('InstaBlam: ', {
body: text,
icon: 'images/icons/camera-apple-touch.png'
})
}
async function updateCache(request) {
return fetch(request)
.then((response) => {
if (response) {
return caches.open('IB_Cache')
.then((cache) => {
return cache.put(request, response.clone())
.then(() => {
return response;
})
});
}
})
}