forked from JerroldLee/tw93.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
59 lines (53 loc) · 1.57 KB
/
Copy pathsw.js
File metadata and controls
59 lines (53 loc) · 1.57 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
const CACHE_NAME = "tw93-cache-v1";
const FONT_BASE_URL = "https://gw.alipayobjects.com/os/k/jinkai/";
const IMMUTABLE_ASSETS = [
"https://gw.alipayobjects.com/os/k/s3/lightense.min.js",
"https://gw.alicdn.com/imgextra/i4/O1CN01XYYPwL1uheeXASHIQ_!!6000000006069-2-tps-420-420.png", // Favicon/Logo
];
self.addEventListener("install", (event) => {
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
}),
);
}),
);
self.clients.claim();
});
self.addEventListener("fetch", (event) => {
const url = event.request.url;
const isImmutableAsset =
IMMUTABLE_ASSETS.includes(url) ||
url.startsWith(FONT_BASE_URL) ||
url.includes("/css/jinkai.css");
if (isImmutableAsset) {
event.respondWith(
caches.match(event.request).then((cachedResponse) => {
if (cachedResponse) {
return cachedResponse;
}
return fetch(event.request).then((response) => {
if (
!response ||
response.status !== 200 ||
(response.type !== "cors" && response.type !== "basic")
) {
return response;
}
const responseToCache = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseToCache);
});
return response;
});
}),
);
}
});