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); }) ); });