-
Notifications
You must be signed in to change notification settings - Fork 0
/
sibj1-sw.js
41 lines (35 loc) · 1.08 KB
/
sibj1-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
'use strict';
var cacheVersion = 1;
var currentCache = {
offline: 'overwatchoffline' + cacheVersion
};
const offlineUrl = 'index.html';
this.addEventListener('install', event => {
event.waitUntil(
caches.open(currentCache.offline).then(function(cache) {
return cache.addAll([
'.',
offlineUrl
]);
})
);
});
// Menambahkan pengembalian offline ke online
this.addEventListener('fetch', event => {
if (event.request.mode === 'navigate' || (event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html'))) {
event.respondWith(
fetch(event.request.url).catch(error => {
// Kembali ke halaman offline
return caches.match(offlineUrl);
})
);
}
else{
// melakukan respon terhadap apa saja yang dilakukan
event.respondWith(caches.match(event.request)
.then(function (response) {
return response || fetch(event.request);
})
);
}
});