-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
67 lines (64 loc) · 1.56 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
const staticCache = 'static-final-v3';
const dynamicCache = 'dynamic-final-v3';
const assets = [
'/',
'/index.html',
'/js/app.js',
'/js/materialize.min.js',
'/js/script.js',
'/img/profile.png',
'/img/oops.png',
'/css/about.css',
'/css/addtask.css',
'/css/home.css',
'/css/profile.css',
'/css/materialize.min.css',
'https://fonts.googleapis.com/icon?family=Material+Icons',
'https://fonts.gstatic.com/s/materialicons/v55/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2',
'https://fonts.gstatic.com/s/dmserifdisplay/v4/-nFnOHM81r4j6k0gjAW3mujVU2B2G_Bx0g.woff2',
'/pages/fallback.html',
];
self.addEventListener('install', (e) => {
e.waitUntil(
caches.open(staticCache).then((cache) => {
console.log('mencache assets');
cache.addAll(assets);
})
);
console.log('instalasi berhasil');
});
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys().then((keys) => {
return Promise.all(
keys
.filter((key) => key !== staticCache && key !== dynamicCache)
.map((key) => caches.delete(key))
);
})
);
console.log('activate berhasil');
});
self.addEventListener('fetch', (e) => {
e.respondWith(
caches
.match(e.request)
.then((cacheRes) => {
return (
cacheRes ||
fetch(e.request).then((fetchRes) => {
return caches.open(dynamicCache).then((cache) => {
cache.put(e.request.url, fetchRes.clone());
return fetchRes;
});
})
);
})
.catch(() => {
if (e.request.url.indexOf('.html') > -1) {
return caches.match('/pages/fallback.html');
}
})
);
console.log('fetch berhasil');
});