File size: 795 Bytes
fd3e471
 
 
 
 
 
 
 
 
555a112
fd3e471
 
 
 
 
 
 
 
 
3a061d5
 
fd3e471
c854123
fd3e471
 
 
 
 
 
 
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
console.log('[SW] Hello from service-worker.js');

const CACHE_NAME = 'pawmatch-v1';
const urlsToCache = [
  './',                  // 根目錄
  './manifest.json',     // Manifest
  './assets/icon-192.png',
  './assets/icon-512.png'
];

// 安裝事件
self.addEventListener('install', function(event) {
  console.log('[SW] install event');
  event.waitUntil(
    caches.open(CACHE_NAME).then(function(cache) {
      console.log('快取開啟:', CACHE_NAME);
      return cache.addAll(urlsToCache);
    })
  );
});

// fetch 事件攔截
self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request).then(function(response) {
      // 如果快取有,就直接回應;否則就抓網路
      return response || fetch(event.request);
    })
  );
});