Spaces:
Running
on
Zero
Running
on
Zero
Update service-worker.js
Browse files- service-worker.js +26 -9
service-worker.js
CHANGED
@@ -1,13 +1,30 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
});
|
8 |
|
|
|
9 |
self.addEventListener('fetch', function(event) {
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
1 |
+
console.log('[SW] Hello from service-worker.js');
|
2 |
+
|
3 |
+
const CACHE_NAME = 'pawmatch-v1';
|
4 |
+
const urlsToCache = [
|
5 |
+
'./', // 根目錄
|
6 |
+
'./manifest.json', // Manifest
|
7 |
+
'./assets/icon-192.png',
|
8 |
+
'./assets/icon-512.png'
|
9 |
+
];
|
10 |
|
11 |
+
// 安裝事件
|
12 |
+
self.addEventListener('install', function(event) {
|
13 |
+
console.log('[SW] install event');
|
14 |
+
event.waitUntil(
|
15 |
+
caches.open(CACHE_NAME).then(function(cache) {
|
16 |
+
console.log('快取開啟:', CACHE_NAME);
|
17 |
+
return cache.addAll(urlsToCache);
|
18 |
+
})
|
19 |
+
);
|
20 |
});
|
21 |
|
22 |
+
// fetch 事件攔截
|
23 |
self.addEventListener('fetch', function(event) {
|
24 |
+
event.respondWith(
|
25 |
+
caches.match(event.request).then(function(response) {
|
26 |
+
// 如果快取有,就直接回應;否則就抓網路
|
27 |
+
return response || fetch(event.request);
|
28 |
+
})
|
29 |
+
);
|
30 |
+
});
|