-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
189 lines (167 loc) · 4.07 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
const STATIC_CACHE_NAME = "static";
const STATIC_CACHE_VERSION = "v41";
const STATIC_CACHE_ID = `${STATIC_CACHE_NAME}-${STATIC_CACHE_VERSION}`;
// All the files need to be added here manually. I want to avoid
// having build tool to pre-generate this.
const STATIC_ASSETS = [
/* HTML etc. */
"index.html",
"favicon.ico",
"manifest.json",
"README.md",
/* JS */
"index.mjs",
"js/boot.mjs",
"js/canvas.mjs",
"js/cursor.mjs",
"js/dom.mjs",
"js/canvas-utils.mjs",
"js/svg-utils.mjs",
"js/controls/gamepad.mjs",
"js/controls/general.mjs",
"js/controls/keyboard.mjs",
"js/controls/touch.mjs",
"js/state/constants.mjs",
"js/state/state.mjs",
"js/state/storage.mjs",
"js/state/utils.mjs",
"js/state/actions/cam.mjs",
"js/state/actions/canvas.mjs",
"js/state/actions/controls.mjs",
"js/state/actions/cursor.mjs",
"js/state/actions/tool.mjs",
"js/state/actions/ui.mjs",
"js/tools/cam.mjs",
"js/tools/fill.mjs",
"js/tools/pen.mjs",
"js/tools/stamp.mjs",
"js/ui/actions.mjs",
"js/ui/colors.mjs",
"js/ui/global.mjs",
"js/ui/panel.mjs",
"js/ui/tools.mjs",
"js/ui/toast.mjs",
"js/ui/utils.mjs",
"js/ui/variants.mjs",
/* CSS */
"css/cam.css",
"css/canvas.css",
"css/dialog.css",
"css/loader.css",
"css/main.css",
"css/panel.css",
"css/toast.css",
/* Images */
"img/logo.png",
"img/icon.png",
"img/screenshot-desktop.png",
"img/buttons/clear.svg",
"img/buttons/info.svg",
"img/buttons/save.svg",
"img/stamps/baby.svg",
"img/stamps/boy.svg",
"img/stamps/girl.svg",
"img/stamps/house.svg",
"img/stamps/man.svg",
"img/stamps/slot.svg",
"img/stamps/star.svg",
"img/stamps/tree.svg",
"img/stamps/truck.svg",
"img/stamps/woman.svg",
"img/tools/cam-cancel.svg",
"img/tools/cam-take-photo.svg",
"img/tools/cam.svg",
"img/tools/fill.svg",
"img/tools/pen-stroke-width-1.svg",
"img/tools/pen-stroke-width-2.svg",
"img/tools/pen-stroke-width-3.svg",
"img/tools/pen.svg",
"img/tools/stamp.svg",
];
function handleRejectedRequest(error) {
console.error(error);
return new Response(
"<h1>You are offline.</h1><p>The application needs to go online at least once to work properly</p>",
{
headers: {
"Content-Type": "text/html; charset=utf-8",
},
},
);
}
async function handleRequest(response, request) {
if (response) {
return response;
}
if (!response) {
console.log(`cache miss for ${request.url}`);
}
return fetch(request)
.then((fetchResponse) => fetchResponse)
.catch(handleRejectedRequest);
}
function onFetch(event) {
const { request } = event;
return event.respondWith(
caches.match(request).then((response) => handleRequest(response, request)),
);
}
function handleCacheOpen(staticCache) {
return staticCache.addAll(STATIC_ASSETS);
}
function handleCacheOpenError(error) {
console.error(error);
return Promise.reject(error);
}
function handleCacheUpdate(cacheNames) {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== STATIC_CACHE_ID) {
console.log(`Deleting cache: ${cacheName}`);
return caches.delete(cacheName);
}
}),
);
}
function finishCacheUpdate() {
return self.clients.claim();
}
function handleCacheUpdateError(error) {
console.error(error);
return Promise.reject(error);
}
function handleMessage(event) {
const type = event.data.type;
if (!type) {
return;
}
switch (type) {
case "SKIP_WAITING": {
self.skipWaiting();
break;
}
default:
break;
}
}
addEventListener("install", function onInstall(event) {
console.log("Installing service worker");
return event.waitUntil(
caches
.open(STATIC_CACHE_ID)
.then(handleCacheOpen)
.catch(handleCacheOpenError),
);
});
addEventListener("activate", function onActivate(event) {
console.log("Activating service worker");
return event.waitUntil(
caches
.keys()
.then(handleCacheUpdate)
.then(finishCacheUpdate)
.catch(handleCacheUpdateError),
);
});
addEventListener("fetch", onFetch);
addEventListener("message", handleMessage);