/
serviceWorker.txt
58 lines (57 loc) · 2.38 KB
/
serviceWorker.txt
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
// on hold pending service worker type dependencies achieve compatibility with TypeScript 3
//
// convert to .ts but do not move to api directory. This must be a separate file not integrated into dom.js
// * add @types/service_worker_api to package.json
//
// service worker support on hold until compatibility is achieved with TypeScript 3
// * this follow comments code goes into dom.js to initiate the request
// if ("serviceWorker" in navigator) {
// navigator.serviceWorker.register("/js/api/serviceWorker.js")
// .then(function dom_load_serviceRegister(reg) {
// // registration worked
// console.log("Registration succeeded. Scope is " + reg.scope);
// })
// .catch(function dom_load_serviceFail(error) {
// // registration failed
// console.log("Registration failed with " + error);
// });
// }
self.oninstall = function worker_install(event) {
event.waitUntil(caches.open("v1").then(function worker_install_open(cache) {
return cache.addAll([
"/",
"/documentation.xhtml",
"/css/color_canvas.css",
"/css/color_shadow.css",
"/css/color_white.css",
"/css/documentation.css",
"/css/global.css",
"/css/index.css",
"/css/page_specific.css",
"/css/reports.css",
"/css/webtool_only.css",
"/node_modules/parse-framework/js/browser.js",
"/js/dom.js"
]);
}));
};
self.onfetch = function service_fetch(event) {
event.respondWith(caches.match(event.request).then(function worker_fetch_respond(response) {
// caches.match() always resolves
// but in case of success response will have value
if (response !== undefined) {
return response;
} else {
return fetch(event.request).then(function worker_fetch_respond_response(response) {
// response may be used only once
// we need to save clone to put one copy in cache
// and serve second one
let responseClone = response.clone();
caches.open("v1").then(function worker_fetch_respond_response_open(cache) {
cache.put(event.request, responseClone);
});
return response;
});
}
}));
};