DawnC commited on
Commit
55b257d
1 Parent(s): 1128e16

Update service-worker.js

Browse files
Files changed (1) hide show
  1. service-worker.js +32 -24
service-worker.js CHANGED
@@ -1,36 +1,44 @@
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
  });
 
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
  });