-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsw.js
246 lines (203 loc) · 7.05 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const APP_VERSION = 6.3;
const DOC_CACHE_NAME = `DOC_CACHE`;
let DOC_CACHE = null;
const RES_CACHE_VERSION = 6.3;
const RES_CACHE_NAME = `RES_CACHEv${RES_CACHE_VERSION.toFixed(2)}`;
let RES_CACHE = null;
const STOP_CACHING = self.registration.scope.includes("127.0.0.1");
// UTILITY FUNCTIONS
/**
* Check if a given string ends with any substring.
*
* @param {string} str
* @param {string[]} ends
* @returns {boolean}
*/
function endsWithAny(str, ends) {
return ends.some((end) => str.endsWith(end));
}
/**
* Print to the console when in development.
*
* @param {string} text
* @param {string} backgroundColor
*/
function debug(text, backgroundColor) {
if (self.registration.scope.includes("127")) {
console.log(
`%c${text}`,
`color: black !important; background-color: ${backgroundColor};`
);
}
}
// SERVICE WORKER LIFECYCLE EVENTS
self.addEventListener("install", (event) => {
debug("Service Worker Installed");
self.skipWaiting();
});
self.addEventListener("activate", async (event) => {
debug("Service Worker Activated");
// Create the two cachess
event.waitUntil(
(async () => {
await clients.claim();
})()
);
// Remove other versions
let cache_names = await caches.keys();
cache_names.forEach((cache_name) => {
if (cache_name != DOC_CACHE_NAME && cache_name != RES_CACHE_NAME) {
debug(`Service Worker => Deleting '${cache_name}'`, "red");
caches.delete(cache_name);
}
});
});
self.addEventListener("fetch", (event) => {
event.respondWith(STOP_CACHING ? fetch(event.request) : get_request(event));
});
async function get_request(request_event) {
const request = request_event.request;
const url = new URL(request.url).origin + new URL(request.url).pathname;
if (url.includes("/gtag/")) {
return fetch(request);
}
if (RES_CACHE == null) {
DOC_CACHE = await caches.open(DOC_CACHE_NAME);
RES_CACHE = await caches.open(RES_CACHE_NAME);
}
if (url.includes("menu.json")) {
let menu = await getMenu(request_event);
return menu;
}
const resourceSuffixes = [".js", ".html", ".css", "/", "manifest.json"];
if (endsWithAny(url, resourceSuffixes)) {
debug(`DOC: ${url}`);
// Check if cached version exists
let cache_match = await DOC_CACHE.match(request, { ignoreVary: true });
if (cache_match) {
// Perform network match in the background
networkFetchAndSave(request, DOC_CACHE);
// Return the cached version
return cache_match;
} else {
debug(`${url} wasn't in cache, so doing fetch`, "yellow");
// Nothing in cache, perform basic fetch request
return fetch(request).then((response) => {
saveResponse(request, response, DOC_CACHE);
return response;
});
}
} else {
debug(`RES: ${url}`);
// Resource like image, manifest etc.
let cache_match = await RES_CACHE.match(request, { ignoreVary: true });
if (cache_match) {
debug(`${url} returned as cache`, "rgb(0, 255, 128)");
return cache_match;
}
// log(`${url} wasn't in cache, so doing fetch`)
return fetch(request).then((response) => {
saveResponse(request, response, RES_CACHE);
return response;
});
}
return new Response(0);
}
/**
* @param {Event} request_event
*/
async function getMenu(request_event) {
const request = request_event.request;
const url = request.url;
if (RES_CACHE == null) {
DOC_CACHE = await caches.open(DOC_CACHE_NAME);
RES_CACHE = await caches.open(RES_CACHE_NAME);
}
// Check if AbortController is supported
if (AbortController !== undefined) {
// AbortController is supported, begin network fetch
let abort_controller = new AbortController();
let signal = abort_controller.signal;
let timeout_id = setTimeout(() => abort_controller.abort(), 2500);
let menu = await fetch(request, { signal: signal })
.then((response) => {
if (response.status == 404) throw new Error();
return response;
})
.then((response) => {
/* Network Match succeeded */
// Clear the abort timeout
clearTimeout(timeout_id);
saveResponse(request, response, RES_CACHE);
// Return network match
debug("(menu) network fetch succeeded", "rgb(128, 255, 128)");
return response;
})
.catch(async (err) => {
debug("(menu) could not fetch in time", "rgb(255, 128, 128)");
// Perform network request in the background
networkFetchAndSave(request, RES_CACHE);
/* Try for cache match */
let cache_match = await RES_CACHE.match(request, {
ignoreVary: true,
});
if (cache_match) {
debug("(menu) Got from cache", "rgb(128, 128, 255)");
}
return cache_match;
});
// Return the cached version
return menu;
}
// At this point we know AbortController is not supported
return await fetch(request)
.then((response) => {
if (response.status == 404) throw new Error();
return response;
})
.then((response) => {
/* Network Match succeeded */
saveResponse(request, response, RES_CACHE);
// Return network match
debug("(menu) network fetch succeeded", "rgb(128, 255, 128)");
return response;
})
.catch(async (err) => {
debug("could not fetch menu in time", "rgb(255, 128, 128)");
// Perform network request in the background
networkFetchAndSave(request, RES_CACHE);
/* Try for cache match */
let cache_match = await RES_CACHE.match(request, {
ignoreVary: true,
});
if (cache_match) {
debug("Got a valid cache match", "rgb(128, 128, 255)");
}
return cache_match;
});
}
/**
* Saves a copy of the response to the given cache
* @param {Request} request
* @param {Response} request
* @param {Cache} cache
*/
function saveResponse(request, response, cache) {
let clone = response.clone();
cache.put(request, clone);
}
/**
* Performs a network fetch for the request and saves it to the given cache
* Asynchronously handled, so you can call this function and forget about it
* @param {Request} request
* @param {Cache} cache
*/
function networkFetchAndSave(request, cache) {
fetch(request)
.then((response) => {
if (response.status == 404) return undefined;
if (!response) return undefined;
cache.put(request, response);
})
.catch((err) => err);
}