-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathservice-worker.js
195 lines (177 loc) · 5.71 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
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
/**
* @fileOverview Provides an asynchronous service worker to manage application behavior.
*
* Service workers are capable of intercepting and adjusting all requests
* before they are sent and after they return.
*
* Services workers can manage caching files to improve performance,
* provide offline app experiences, enable 'home page' installation on devices,
* and provide push notifications to your users.
*
* An updated Service worker will download (upon first page access, or every 24 hours).
* If new, it will yield the install event.
* It will yield the activate event after pages load and old service worker is no longer used.
*
* Use Chrome Dev Tools / Application to clear storage.
*
* Use Chrome Dev Tools / Application / ServiceWorker to debug.
*
* Check "update on reload" to force service worker update on page reload.
*
* Use Chrome Dev Tools / Audit to evaluate.
*
* JSDoc comments are written in Markdown.
*
* @author Denise Case
*
* @requires EXTERNAL:@link{https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js}
*/
// eslint-disable-next-line no-undef
importScripts(
'https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js',
);
if (workbox) {
const appName = '44-563-webapps-syllabus';
const appVersion = 'v1';
const maxAge10MinInSeconds = 10 * 60;
const maxAge1DayInSeconds = 1 * 24 * 60 * 60;
const httpReponseOk = 200; // good
// test Regular Expressions at https://regexr.com/
const reStatic = /\.(?:js|css|html)$/;
const reImages = /\.(?:png|gif|jpg|jpeg|webp|svg)$/;
const reCdnFont = /https:\/\/use\.fontawesome\.com\/.*all\.css$/;
const reCdnStyles = /https:\/\/stackpath\.bootstrapcdn\.com\/.*\.css$/;
const reSyllabus = /https:\/\/denisecase.github.io\/*.(?:js|css|html)$/;
// set a prefix & suffix so local host caches remain unique
workbox.core.setCacheNameDetails({
prefix: appName,
suffix: appVersion,
precache: 'pre-cache',
runtime: 'runtime-cache',
});
const precacheCacheName = workbox.core.cacheNames.precache;
// CDN Fonts:
// use stale cached files while downloading new for next time
// set the max age of the cached files
workbox.routing.registerRoute(
reCdnFont,
new workbox.strategies.StaleWhileRevalidate({
cacheName: precacheCacheName,
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
maxAgeSeconds: maxAge10MinInSeconds,
}),
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [httpReponseOk],
}),
],
}),
);
workbox.routing.registerRoute(
reSyllabus,
new workbox.strategies.StaleWhileRevalidate({
cacheName: precacheCacheName,
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
maxAgeSeconds: maxAge10MinInSeconds,
}),
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [httpReponseOk],
}),
],
}),
);
// CDN styles:
// use stale cached files while downloading new for next time
// set the max age of the cached files
workbox.routing.registerRoute(
reCdnStyles,
new workbox.strategies.StaleWhileRevalidate({
cacheName: precacheCacheName,
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
maxAgeSeconds: maxAge1DayInSeconds,
}),
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [httpReponseOk],
}),
],
}),
);
// Static assets:
// use stale cached files while downloading new for next time
// set the max age of the cached files
workbox.routing.registerRoute(
reStatic,
new workbox.strategies.StaleWhileRevalidate({
cacheName: precacheCacheName,
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
maxAgeSeconds: maxAge10MinInSeconds,
}),
],
}),
);
// Images:
// use stale cached files while downloading new for next time
// set the max age of the cached files
// use different cache for images
workbox.routing.registerRoute(
reImages,
new workbox.strategies.StaleWhileRevalidate({
cacheName: `${precacheCacheName}-images`,
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
maxAgeSeconds: maxAge10MinInSeconds,
}),
],
}),
);
// Define a common handler if any of the fetching methods fail
workbox.routing.setCatchHandler(({ event }) => {
console.error(`Error: ${event.error}`);
if (event.request.mode === 'navigate') {
return caches.match('/error-page.html');
}
return Response.error();
});
// respond with 200 (ok) even when offline
this.addEventListener('install', (event) => {
event.waitUntil(
caches
.open(precacheCacheName)
.then((cache) =>
cache.addAll([
'index.html',
'init-contributions.js',
'init-outline.js',
'init-outcomes.js',
'scripts/register-sw.js',
'custom-elements/nw-syllabus-contributions.js',
'custom-elements/nw-syllabus-outcomes-list.js',
'custom-elements/nw-syllabus-outline.js',
]),
)
.catch((error) => {
console.error(`Error in install event: ${error} `);
}),
);
});
this.addEventListener('fetch', (event) => {
event.respondWith(
caches
.match(event.request)
.then((response) => {
if (response) {
return response;
}
return fetch(event.request);
})
.catch((error) => {
console.error(`Error on fetch: ${error} `);
}),
);
});
} else {
console.error('Error: Workbox did not load.');
}