Spaces:
Running
on
Zero
Running
on
Zero
Update service-worker.js
Browse files- service-worker.js +32 -24
service-worker.js
CHANGED
@@ -1,36 +1,44 @@
|
|
1 |
const CACHE_NAME = 'pawmatch-v1';
|
2 |
const urlsToCache = [
|
3 |
-
'
|
4 |
-
'
|
5 |
-
'
|
6 |
-
'
|
7 |
];
|
8 |
|
9 |
-
self.addEventListener('install',
|
10 |
-
console.log('[SW] Service Worker 安裝中...');
|
11 |
event.waitUntil(
|
12 |
-
|
13 |
-
.then(
|
14 |
-
console.log('[SW] 快取開啟成功');
|
15 |
return cache.addAll(urlsToCache);
|
16 |
-
})
|
17 |
-
.
|
18 |
-
|
19 |
-
})
|
20 |
);
|
21 |
});
|
22 |
|
23 |
-
self.addEventListener('
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
event.respondWith(
|
26 |
-
caches.match(event.request)
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
console.log('[SW] 從網路取得:', event.request.url);
|
33 |
-
return fetch(event.request);
|
34 |
-
})
|
35 |
);
|
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', (event) => {
|
|
|
10 |
event.waitUntil(
|
11 |
+
Promise.all([
|
12 |
+
caches.open(CACHE_NAME).then((cache) => {
|
|
|
13 |
return cache.addAll(urlsToCache);
|
14 |
+
}),
|
15 |
+
self.skipWaiting()
|
16 |
+
])
|
|
|
17 |
);
|
18 |
});
|
19 |
|
20 |
+
self.addEventListener('activate', (event) => {
|
21 |
+
event.waitUntil(
|
22 |
+
Promise.all([
|
23 |
+
caches.keys().then((cacheNames) => {
|
24 |
+
return Promise.all(
|
25 |
+
cacheNames
|
26 |
+
.filter((cacheName) => cacheName !== CACHE_NAME)
|
27 |
+
.map((cacheName) => caches.delete(cacheName))
|
28 |
+
);
|
29 |
+
}),
|
30 |
+
self.clients.claim()
|
31 |
+
])
|
32 |
+
);
|
33 |
+
});
|
34 |
+
|
35 |
+
self.addEventListener('fetch', (event) => {
|
36 |
event.respondWith(
|
37 |
+
caches.match(event.request).then((response) => {
|
38 |
+
if (response) {
|
39 |
+
return response;
|
40 |
+
}
|
41 |
+
return fetch(event.request);
|
42 |
+
})
|
|
|
|
|
|
|
43 |
);
|
44 |
});
|