-
Notifications
You must be signed in to change notification settings - Fork 5
/
sw.js
70 lines (70 loc) · 2.17 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
((_) => {
"use strict";
const self = _;
const hostname = self.location.hostname;
const cacheName = "167e1f07-b59a-4742-bb45-15cf3caabcce";
async function optFetch(request) {
try {
return await self.fetch(request);
} catch (err) {
return new Response(void 0, {
status: 500
});
}
}
async function handleFetch(e) {
const request = e.request;
const url = new URL(request.url);
switch (url.protocol) {
case "http:":
case "https:":
break;
default:
return await self.fetch(request);
}
switch (request.method) {
case "GET":
case "HEAD":
break;
default:
return await optFetch(request);
}
if (url.pathname === "/T-Crack/manifest.json")
return await optFetch(request);
const cached = await caches.match(request, {
cacheName
});
if (cached != null)
return cached;
const response = await e.preloadResponse || await optFetch(request);
if (hostname !== "localhost") {
const cache = await caches.open(cacheName);
await cache.put(request, response.clone());
}
return response;
}
async function handleMessage(e) {}
async function handleInstall(e) {
const cache = await caches.open(cacheName);
await cache.addAll(["sw.js", "manifest.json"]);
}
async function handleActivate(e) {
await self.clients.claim();
for (const k of await caches.keys()) {
if (k != cacheName)
await caches.delete(k);
}
}
self.addEventListener("fetch", (e) => e.respondWith(handleFetch(e)), {
passive: true
});
self.addEventListener("message", (e) => e.waitUntil(handleMessage(e)), {
passive: true
});
self.addEventListener("install", (e) => e.waitUntil(handleInstall(e)), {
passive: true
});
self.addEventListener("activate", (e) => e.waitUntil(handleActivate(e)), {
passive: true
});
})(self);