-
Notifications
You must be signed in to change notification settings - Fork 4
/
service-worker.js
108 lines (93 loc) · 4.04 KB
/
service-worker.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
106
107
108
const runtime_cache_ = 'runtime';
let precache_ = ""
// every check-in should change this value for refresh cache.
let hash = '1/23:14:01/2021';
self.addEventListener('install', (event) => {
let t = new Date();
precache_ = "precache_" + t.getTime();
event.waitUntil(
caches.open(precache_)
.then(cache => {
cache.addAll(sites_v1);
cache.addAll(persistent_image_v1);
})
.then(self.skipWaiting()) // skipWaiting.
);
});
self.addEventListener('activate', (event) => {
// New service worker upgrade, then delete caches other than whitelist..
const whiteList = [precache_];
event.waitUntil(
// if (self.registration.navigationPreload) {
// await self.registration.navigationPreload.enable();
// }
caches.keys().then(cacheNames => {
return cacheNames.filter(cacheName => !whiteList.includes(cacheName));
}).then(cachesToDelete => {
return Promise.all(cachesToDelete.map(cacheToDelete => {
return caches.delete(cacheToDelete);
}));
}).then(() => self.clients.claim()) // clients.claim()
);
});
self.addEventListener('fetch', (event) => {
if (event.request.url.startsWith(self.location.origin)) {
var path = event.request.url;
// if (sites_v1.indexOf(path) !== -1 ||
// persistent_image_v1.indexOf(path) !== -1) {
// defer checking caches.
event.respondWith(
caches.match(event.request).then(cachedResponse => {
if (cachedResponse) {
return cachedResponse;
}
// const preload_response = await event.preloadResponse;
// if (preload_response) {
// storeToCache(preload_response.clone());
// return preload_response;
// }
return fetch(event.request).then(
function(response) {
// Check if we received a valid response
if(!response || response.status !== 200 || response.type !== 'basic') {
// null body returns will let client fetch on the client.
return new Response();
}
function storeToCache(response) {
caches.open(runtime_cache_)
.then(function(cache) {
cache.put(event.request, response);
});
}
// IMPORTANT: Clone the response. A response is a stream
// and because we want the browser to consume the response
// as well as the cache consuming the response, we need
// to clone it so we have two streams.
var responseToCache = response.clone();
storeToCache(response.clone());
return response;
}
).catch(e => {
console.log('failed to fetch');
});
})
);
// }
}
});
var sites_v1 = [
"https://sunggook.github.io/hello-pwa/service-worker.js",
"https://sunggook.github.io/hello-pwa/",
"https://sunggook.github.io/hello-pwa/script.js",
"https://sunggook.github.io/hello-pwa/share.js",
"https://sunggook.github.io/hello-pwa/badging.js",
"https://sunggook.github.io/hello-pwa/style.css",
"https://sunggook.github.io/hello-pwa/related_apps.js",
"https://sunggook.github.io/hello-pwa/simple-text-editor/filehandler.js",
"https://sunggook.github.io/hello-pwa/simple-text-editor/file_editor.js",
"https://sunggook.github.io/hello-pwa/manifest.json",
]
var persistent_image_v1 = [
"https://sunggook.github.io/hello-pwa/logo256.png",
"https://sunggook.github.io/hello-pwa/favicon.ico",
]