-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
53 lines (50 loc) · 1.24 KB
/
sw.js
File metadata and controls
53 lines (50 loc) · 1.24 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
const CACHE_NAME = 'year-progress-v3';
const ASSETS_TO_CACHE = [
'/',
'/index.html',
'/config.js',
'/site.webmanifest',
'/favicon.ico',
'/img/favicon-16x16.png',
'/img/favicon-32x32.png',
'/img/apple-touch-icon.png',
'/img/android-chrome-192x192.png',
'/img/android-chrome-512x512.png'
];
// 安装 SW 并缓存资源
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log('SW: Caching assets');
return cache.addAll(ASSETS_TO_CACHE);
})
);
});
// 激活 SW 并清理旧缓存
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cache) => {
if (cache !== CACHE_NAME) {
console.log('SW: Clearing old cache');
return caches.delete(cache);
}
})
);
})
);
});
// 拦截请求:缓存优先策略
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
// 如果缓存中有,直接返回
if (response) {
return response;
}
// 否则从网络请求
return fetch(event.request);
})
);
});