-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
93 lines (87 loc) · 2.6 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
const preCache = [
"/images/taichi.png",
"/images/banner.webp",
"/images/taichi-fill.png",
"/css/loader.css",
"/css/style.css",
"/js/script.js",
];
const VERSION = "1736048183185";
const cacheDomain = [
"fonts.googleapis.com",
"npm.webcache.cn",
"unpkg.com",
"fastly.jsdelivr.net",
"cdn.jsdelivr.net",
];
// 安装时预加载必要内容
self.addEventListener("install", (event) => {
console.log(`Service Worker ${VERSION} installing.`);
event.waitUntil(caches.open(VERSION).then((cache) => cache.addAll(preCache)));
});
async function cacheRequest(request, options) {
try {
const responseToCache = await fetch(request);
const cache = await caches.open(VERSION);
if (!/^https?:$/i.test(new URL(request.url).protocol))
return responseToCache;
cache.put(request, responseToCache.clone());
return responseToCache;
} catch (e) {
const responseToCache = await fetch(request, options);
const cache = await caches.open(VERSION);
if (!/^https?:$/i.test(new URL(request.url).protocol))
return responseToCache;
cache.put(request, responseToCache.clone());
return responseToCache;
}
}
async function respondRequest(request, options) {
const response = await caches.match(request);
if (response) {
return response;
}
return cacheRequest(request, options);
}
self.addEventListener("fetch", (event) => {
const url = new URL(event.request.url);
// 检查请求的域名是否在 CacheDomain 中
if (cacheDomain.includes(url.hostname)) {
event.respondWith(respondRequest(event.request));
} else {
// 检查请求是否为 POST 或带有查询参数的 GET 这样可避免错误缓存
if (
event.request.method === "POST" ||
(event.request.method === "GET" && url.search)
) {
try {
event.respondWith(fetch(event.request));
} catch (e) {
event.respondWith(fetch(event.request, { mode: "no-cors" }));
}
} else {
event.respondWith(respondRequest(event.request, { mode: "no-cors" }));
}
}
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (VERSION !== cacheName) {
console.log(`Service Worker: deleting old cache ${cacheName}`);
return caches.delete(cacheName);
}
})
);
})
);
console.log(`Service Worker ${VERSION} activated.`);
});
self.addEventListener("message", (event) => {
console.log("Service Worker: message received");
if (event.data === "skipWaiting") {
self.skipWaiting();
}
});