DawnC commited on
Commit
ab0922f
·
verified ·
1 Parent(s): 3269018

Create service-worker.js

Browse files
Files changed (1) hide show
  1. service-worker.js +36 -0
service-worker.js ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const CACHE_NAME = 'pawmatch-v1';
2
+ const urlsToCache = [
3
+ './',
4
+ './manifest.json',
5
+ './icon-192.png',
6
+ './icon-512.png'
7
+ ];
8
+
9
+ self.addEventListener('install', function(event) {
10
+ console.log('[SW] Service Worker 安裝中...');
11
+ event.waitUntil(
12
+ caches.open(CACHE_NAME)
13
+ .then(function(cache) {
14
+ console.log('[SW] 快取開啟成功');
15
+ return cache.addAll(urlsToCache);
16
+ })
17
+ .catch(function(error) {
18
+ console.error('[SW] 快取失敗:', error);
19
+ })
20
+ );
21
+ });
22
+
23
+ self.addEventListener('fetch', function(event) {
24
+ console.log('[SW] 攔截請求:', event.request.url);
25
+ event.respondWith(
26
+ caches.match(event.request)
27
+ .then(function(response) {
28
+ if (response) {
29
+ console.log('[SW] 從快取回應:', event.request.url);
30
+ return response;
31
+ }
32
+ console.log('[SW] 從網路取得:', event.request.url);
33
+ return fetch(event.request);
34
+ })
35
+ );
36
+ });