Spaces:
Running
on
Zero
Running
on
Zero
Update service-worker.js
Browse files- service-worker.js +21 -35
service-worker.js
CHANGED
@@ -1,42 +1,28 @@
|
|
1 |
const CACHE_NAME = 'pawmatch-v1';
|
2 |
-
|
3 |
-
// 需要快取的檔案清單
|
4 |
const urlsToCache = [
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
];
|
10 |
|
11 |
-
//
|
12 |
-
self.addEventListener('install', event
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
console.error('快取檔案失敗:', error);
|
21 |
-
})
|
22 |
-
);
|
23 |
});
|
24 |
|
25 |
-
//
|
26 |
-
self.addEventListener('fetch', event
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
}
|
34 |
-
// 否則發送網路請求
|
35 |
-
return fetch(event.request);
|
36 |
-
})
|
37 |
-
.catch(error => {
|
38 |
-
console.error('讀取檔案失敗:', error);
|
39 |
-
return fetch(event.request);
|
40 |
-
})
|
41 |
-
);
|
42 |
});
|
|
|
1 |
const CACHE_NAME = 'pawmatch-v1';
|
|
|
|
|
2 |
const urlsToCache = [
|
3 |
+
'./',
|
4 |
+
'./manifest.json',
|
5 |
+
'./assets/icon-192.png',
|
6 |
+
'./assets/icon-512.png'
|
7 |
];
|
8 |
|
9 |
+
// 安裝事件處理器
|
10 |
+
self.addEventListener('install', function(event) {
|
11 |
+
event.waitUntil(
|
12 |
+
caches.open(CACHE_NAME)
|
13 |
+
.then(function(cache) {
|
14 |
+
console.log('快取開啟');
|
15 |
+
return cache.addAll(urlsToCache);
|
16 |
+
})
|
17 |
+
);
|
|
|
|
|
|
|
18 |
});
|
19 |
|
20 |
+
// 請求處理器
|
21 |
+
self.addEventListener('fetch', function(event) {
|
22 |
+
event.respondWith(
|
23 |
+
caches.match(event.request)
|
24 |
+
.then(function(response) {
|
25 |
+
return response || fetch(event.request);
|
26 |
+
})
|
27 |
+
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
});
|