-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserviceWorker.js
More file actions
291 lines (242 loc) · 11.1 KB
/
Copy pathserviceWorker.js
File metadata and controls
291 lines (242 loc) · 11.1 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
const INSTALLATION_VERSION_NAME = "1.4.0-r202605121447";
// ^^ Use for check new update "Native application(webview) version(or Android/iOS version combo) - PWA release version"
// ex) "1.0.1/1.0.0-r20251101k"
const INSTALLATION_FILE_LIST = [
"./serviceWorker.js",
"./fixedTop.html",
"./fixedBottom.html",
"./mainMenu.html",
"./staticDoc.html",
"./instantDoc.html",
"./managedOverlay.html",
"./overwatchPanel.html",
"./customHandlePrototypes.html",
"./styles/main.css",
"./scripts/main.js",
];
// Common files cache - Be changes some time but, well not changed very often
const CACHE_NAME_COMMON_FILES = "common-files-cache-v1-20260424";
const COMMON_FILES_TO_CACHE = [
"./",
"./index.html",
"./serviceLoader.html",
"./stockHandlePrototypes.html",
"./webmanifest.json",
"./styles/estreUiInitialize.css",
"./styles/estreUiEmoji.css",
"./styles/estreUiRoot.css",
"./styles/estreUiCore.css",
"./styles/estreUiCore2.css",
"./styles/estreUiAliases.css",
"./styles/estreUi.css",
"./styles/estreUiHandles.css",
"./styles/estreUiHandleUnical.css",
"./scripts/lib/jcodd.js",
"./scripts/lib/doctre.js",
"./scripts/lib/modernism.js",
"./scripts/lib/alienese.js",
"./scripts/estreU0EEOZ.js",
"./scripts/estreUi-core.js",
"./scripts/estreUi-dialog.js",
"./scripts/estreUi-notation.js",
"./scripts/estreUi-notification.js",
"./scripts/estreUi-pageModel.js",
"./scripts/estreUi-pageManager.js",
"./scripts/estreUi-handles.js",
"./scripts/estreUi-interaction.js",
"./scripts/estreUi-main.js",
];
// Static files cache - Rarely changes after release
const CACHE_NAME_STATIC_FILES = "static-files-cache-v1-20260221";
const STATIC_FILES_TO_CACHE = [
"./favicon.ico",
"https://raw.githack.com/googlefonts/noto-emoji/main/fonts/NotoColorEmoji.ttf",
"./images/app_icon_16x16.png",
"./images/app_icon_32x32.png",
"./images/app_icon_48x48.png",
"./images/app_icon_144x144.png",
"./images/app_icon_167x167.png",
"./images/app_icon_180x180.png",
"./images/app_icon_192x192.png",
"./images/app_icon_512x512.png",
"https://fonts.googleapis.com/css2?family=Cute+Font&family=Noto+Sans+KR:wght@100..900&display=swap",
// "https://code.jquery.com/jquery-3.7.1.js",
// "https://code.jquery.com/jquery-4.0.0.slim.min.js",
"https://code.jquery.com/jquery-4.0.0.min.js",
"https://unpkg.com/@dotlottie/player-component@latest/dist/dotlottie-player.mjs",
"./lotties/menu_and_back_btn.json",
"./lotties/menu_and_close_btn.json",
"./lotties/loading_circle.json",
"./lotties/ptr_indic.json",
"./lotties/progress_bar.json",
"./vectors/more_vertical_slim_icon.svg",
"./vectors/app_icon.svg",
];
// Stony files cache - Very frequently changes and heavy sized files (for background caching)
const CACHE_NAME_STONY_FILES = "stony-files-cache-v1-20251101b";
const STONY_FILES_TO_CACHE = [
];
const CHECK_ALWAYS_NEWER_FILE_LIST = [
"./serviceWorker.js",
];
const EMPTY_RESPONSE = new Response(null, {
status: 200,
headers: {
"Content-Type": "octet/stream",
"Content-Length": "0",
}
});
self.isLog = true;
self.isDebug = false;
self.isVerbose = false;
Object.defineProperty(self, "isLogging", {
"get": function () { return this.isLog || this.isDebug; },
configurable: true,
enumerable: false,
});
Object.defineProperty(self, "isVerbosely", {
"get": function () { return this.isDebug && this.isVerbose; },
configurable: true,
enumerable: false,
});
self.addEventListener("install", (event) => {
if (self.isLogging) console.log("Service Worker - Install service worker with cache list" + INSTALLATION_VERSION_NAME);
const scope = self.registration.scope;
// vv When use for force installing the new service worker always (not recommended)
//self.skipWaiting();
event.waitUntil(
caches.keys().then(async keyList => {
const loaders = [];
if (!keyList.includes(CACHE_NAME_COMMON_FILES)) {
if (self.isLogging) console.log("Service Worker - Caching common files - " + CACHE_NAME_COMMON_FILES);
loaders.push(caches.open(CACHE_NAME_COMMON_FILES).then((cache) => cache.addAll(COMMON_FILES_TO_CACHE.map(it => it.replace(/^\.\//, scope)))));
}
if (!keyList.includes(CACHE_NAME_STATIC_FILES)) {
if (self.isLogging) console.log("Service Worker - Caching static files - " + CACHE_NAME_STATIC_FILES);
loaders.push(caches.open(CACHE_NAME_STATIC_FILES).then((cache) => cache.addAll(STATIC_FILES_TO_CACHE.map(it => it.replace(/^\.\//, scope)))));
}
if (!keyList.includes(INSTALLATION_VERSION_NAME)) {
if (self.isLogging) console.log("Service Worker - Caching application files - " + INSTALLATION_VERSION_NAME);
loaders.push(caches.open(INSTALLATION_VERSION_NAME).then((cache) => cache.addAll(INSTALLATION_FILE_LIST.map(it => it.replace(/^\.\//, scope)))));
}
await Promise.all(loaders);
if (self.isLogging) console.log("Service Worker - All required files are cached");
if (!keyList.includes(CACHE_NAME_STATIC_FILES)) (async _ => {
if (self.isLogging) console.log("Service Worker - Caching static files - " + CACHE_NAME_STATIC_FILES);
await caches.open(CACHE_NAME_STATIC_FILES).then((cache) => cache.addAll(STATIC_FILES_TO_CACHE.map(it => it.replace(/^\.\//, scope))));
if (self.isLogging) console.log("Service Worker - Static files are cached");
})();
return;
})
);
});
self.addEventListener("activate", (event) => {
if (self.isLogging) console.log("Service Worker - Begin service worker with " + INSTALLATION_VERSION_NAME);
event.waitUntil(
caches.keys().then((keyList) =>
Promise.all(
keyList.map((key) => {
if (CACHE_NAME_COMMON_FILES !== key && CACHE_NAME_STATIC_FILES !== key && INSTALLATION_VERSION_NAME !== key && CACHE_NAME_STONY_FILES !== key) {
if (self.isLogging) console.log("Service Worker - Clear older cached - " + key);
return caches.delete(key);
}
})
)
)
);
// vv When use for force alternate the fetch interceptor of new service worker without reload page always (not recommended)
// event.waitUntil(self.clients.claim());
});
self.addEventListener("fetch", (event) => {
const scope = self.registration.scope;
const urlString = event.request.url;
const url = new URL(urlString);
const pathname = url.pathname;
const fullpath = url.origin + pathname;
if (CHECK_ALWAYS_NEWER_FILE_LIST.find(it => fullpath == it.replace(/^\.\//, scope))) {
event.respondWith(
fetch(event.request).catch(async (error) => {
if (self.isLogging) console.log("Service Worker - Return cached file by error on fetch: ", event.request.url);
return (await caches.match(event.request)) ?? error;
})
);
} else if (pathname.includes("/|") || pathname.includes("/%7C")) event.respondWith(EMPTY_RESPONSE);
else if (COMMON_FILES_TO_CACHE.find(it => fullpath == it.replace(/^\.\//, scope)) || INSTALLATION_FILE_LIST.find(it => fullpath == it.replace(/^\.\//, scope)) || STATIC_FILES_TO_CACHE.find(it => fullpath == it.replace(/^\.\//, scope)) || STONY_FILES_TO_CACHE.find(it => fullpath == it.replace(/^\.\//, scope))) {
event.respondWith((async () => {
// if (self.isLogging) console.log("Service Worker - Fetch intercepted for: ", urlString);
// Try to get the response from a cache.
const cachedResponse = await caches.match(event.request);
// Return it if we found one.
if (cachedResponse) {
if (self.isLogging) console.log("Service Worker - Return cached file: ", event.request.url);
return cachedResponse;
} else {
// If we didn't find a match in the cache, use the network.
if (self.isLogging) console.log("Service Worker - Not in cache list, try fetch directly: ", event.request.url);
return fetch(event.request).catch(async (error) => {
if (self.isLogging) console.log("Service Worker - Return cached file by error on fetch: ", event.request.url);
return (await caches.match(event.request)) ?? error;
});
}
})());
}
});
self.addEventListener("message", async (event) => {
let response = null;
switch (event.data.type) {
case "SKIP_WAITING":
if (self.isLogging) console.log('Service Worker: SKIP_WAITING received');
self.skipWaiting();
// Never call source for alternate old service worker
return;
case "CLIENTS_CLAIM":
if (self.isLogging) console.log('Service Worker: CLIENTS_CLAIM received');
self.clients.claim();
// Never call source for alternate old service worker
return;
case "clearCache":
response = await caches.delete(INSTALLATION_VERSION_NAME).then(() => {
if (self.isLogging) console.log("Cache cleared: ", INSTALLATION_VERSION_NAME);
});
break;
case "clearCommonCache":
response = await caches.delete(CACHE_NAME_COMMON_FILES).then(returns => {
if (self.isLogging) console.log("Service Worker - Common Cache cleared: ", CACHE_NAME_COMMON_FILES);
return returns;
});
break;
case "clearStaticCache":
response = await caches.delete(CACHE_NAME_STATIC_FILES).then(returns => {
if (self.isLogging) console.log("Service Worker - Static Cache cleared: ", CACHE_NAME_STATIC_FILES);
return returns;
});
break;
case "clearStonyCache":
response = await caches.delete(CACHE_NAME_STONY_FILES).then(returns => {
if (self.isLogging) console.log("Service Worker - Stony Cache cleared: ", CACHE_NAME_STONY_FILES);
return returns;
});
break;
case "clearAllCaches":
response = await caches.keys().then(async keyList => {
if (self.isLogging) console.log("Service Worker - Clear all caches");
const deletions = [];
keyList.forEach(key => {
if (self.isLogging) console.log("Service Worker - Clear cached: " + key);
deletions.push(caches.delete(key));
});
return Promise.all(deletions);
});
break;
case "getVersion":
response = INSTALLATION_VERSION_NAME;
break;
case "getApplicationCount":
response = await self.clients.matchAll({
includeUncontrolled: false,
type: 'window'
}).then(clients => clients.length);
break;
}
event.source.postMessage({ type: "worked", request: event.data, response });
});