File size: 936 Bytes
0f17dbc
 
 
ab0922f
0f17dbc
 
 
 
ab0922f
 
55b257d
0f17dbc
 
 
 
 
 
 
 
 
55b257d
 
 
0f17dbc
 
 
 
 
 
 
 
 
 
 
 
 
ab0922f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const CACHE_NAME = 'pawmatch-v2';
const BASE_URL = self.location.pathname.replace('service-worker.js', '');

const urlsToCache = [
  BASE_URL,
  `${BASE_URL}manifest.json`,
  `${BASE_URL}icon-192.png`,
  `${BASE_URL}icon-512.png`
];

self.addEventListener('install', (event) => {
  event.waitUntil(
    caches.open(CACHE_NAME)
      .then((cache) => {
        console.log('Cache opened');
        return cache.addAll(urlsToCache);
      })
      .then(() => self.skipWaiting())
      .catch(error => console.error('Cache error:', error))
  );
});

self.addEventListener('fetch', (event) => {
  event.respondWith(
    caches.match(event.request)
      .then((response) => {
        if (response) {
          return response;
        }
        return fetch(event.request);
      })
      .catch(() => {
        // 如果離線且快取中沒有,返回離線頁面或預設內容
        return new Response('Offline');
      })
  );
});