-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
39 lines (37 loc) · 1.23 KB
/
service-worker.js
File metadata and controls
39 lines (37 loc) · 1.23 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
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open('youbike-cache-v1').then((cache) => {
return cache.addAll([
'/',
'/YouBike/index.html',
'/YouBike/main.js',
'/YouBike/manifest.json',
]);
}).then(() => self.skipWaiting()) // 加入立即跳過等待
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
if (response) return response;
return fetch(event.request).catch((error) => {
console.error('PWA fetch failed:', error);
throw error;
});
})
);
});
self.addEventListener('activate', (event) => {
const cacheWhitelist = ['youbike-cache-v1'];
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
}).then(() => self.clients.claim()) // 加入立即控制所有客戶端
);
});