-
Notifications
You must be signed in to change notification settings - Fork 4
/
sw.js
161 lines (142 loc) · 4.41 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* jshint worker: true */
/*globals async, caches, siteFiles, Response, fetch */
"use strict";
/* WHY IS THIS FILE AT THE ROOT? */
// Let's check that the SW script loaded
//console.log("Hello, I'm the service worker!");
// Let's start bringing in things we need
importScripts("js/lib/async.js");
// Set up Service Worker tasks
const SWTasks = {
deleteAllCaches: async(function* () {
// Fill me in
}),
cacheSite: async(function* (ev, siteFiles) {
// Fill me in
}),
respondFromCache: async(function* (request, cacheName) {
// Fill me in
}),
hasCacheEntry: async(function* (request, cacheName) {
// Fill me in
}),
};
// Let's set up life cycle listeners ("install", "activate", "fetch")
self.addEventListener("install", async(function* (ev) {
// Fill me in
}));
self.addEventListener("activate", async(function* (ev) {
// Fill me in
}));
self.addEventListener("fetch", function(ev) {
// Fill me in
});
self.addEventListener("message", async(function* (ev) {
// Fill me in
}));
//==========
// "use strict";
// /* WHY IS THIS FILE AT THE ROOT? */
// // Let's check that the SW script loaded
// console.log("Hello, I'm the service worker!");
// // Let's start bringing in things we need
// importScripts("js/lib/async.js");
// importScripts("js/siteFiles.js");
// // Set up Service Worker tasks
// const SWTasks = {
// deleteAllCaches: async(function* () {
// var keys = yield caches.keys();
// var promisesToDelete = keys.map(
// key => caches.delete(key)
// );
// yield Promise.all(promisesToDelete);
// }),
// cacheSite: async(function* (ev, siteFiles) {
// console.log("Trying to offline site");
// try {
// var cache = yield caches.open("site_cache");
// yield cache.addAll(siteFiles);
// } catch (err) {
// console.error(err);
// }
// console.log("Done caching site files!");
// }),
// respondFromCache: async(function* (request, cacheName) {
// var cache = yield caches.open(cacheName);
// var response = yield cache.match(request);
// // We have this, yay!
// if (response) {
// return response;
// }
// // Try to get it from the network and store it
// try {
// response = yield fetch(request);
// yield cache.put(request, response.clone());
// } catch (err) {
// var msg = `failed to store ${request.url || request} in ${cacheName}.`;
// console.warn(msg, err);
// // ensure a response
// response = new Response();
// }
// return response;
// }),
// hasCacheEntry: async(function* (request, cacheName) {
// var cache = yield caches.open(cacheName);
// var response = yield cache.match(request);
// return (response) ? true : false;
// }),
// };
// // Let's set up life cycle listeners ("install", "activate", "fetch")
// self.addEventListener("install", async(function* (ev) {
// console.log("Handling", ev.type);
// yield self.skipWaiting();
// yield SWTasks.deleteAllCaches();
// yield SWTasks.cacheSite(ev, siteFiles);
// console.log("Successfully completed install tasks...");
// }));
// self.addEventListener("activate", async(function* (ev) {
// console.log("Handling", ev.type);
// yield self.clients.claim();
// }));
// self.addEventListener("fetch", function(ev) {
// //console.log("Handling", ev.type, ev.request.url);
// // Responding from cache
// ev.respondWith(SWTasks.respondFromCache(ev.request, "site_cache"));
// });
// self.addEventListener("message", async(function* (ev) {
// var name = ev.data.name || "";
// var id = ev.data.id || "";
// var result;
// switch (name) {
// case "ping":
// result = "pong";
// break;
// case "isFileInCache":
// var url = new URL(ev.data.path, self.location).href;
// console.log("checking for", url);
// try {
// result = yield SWTasks.hasCacheEntry(url, "site_cache");
// } catch (err) {
// console.log("something went wrong", err);
// }
// break;
// default:
// console.warn("No case handler for message?", ev.data);
// }
// var message = {
// name: name,
// id: id,
// result: result,
// };
// if(ev.source){
// ev.source.postMessage(message);
// } else {
// var clients = yield self.clients.matchAll();
// clients.filter(
// client => client.focused
// ).forEach(
// client => client.postMessage(message)
// );
// console.log("We have clients:", clients.length);
// }
// }));