Spaces:
Running
on
Zero
Running
on
Zero
const CACHE_NAME = 'pawmatch-v1'; | |
const urlsToCache = [ | |
'./', | |
'./manifest.json', | |
'./icon-192.png', | |
'./icon-512.png' | |
]; | |
self.addEventListener('install', function(event) { | |
console.log('[SW] Service Worker 安裝中...'); | |
event.waitUntil( | |
caches.open(CACHE_NAME) | |
.then(function(cache) { | |
console.log('[SW] 快取開啟成功'); | |
return cache.addAll(urlsToCache); | |
}) | |
.catch(function(error) { | |
console.error('[SW] 快取失敗:', error); | |
}) | |
); | |
}); | |
self.addEventListener('fetch', function(event) { | |
console.log('[SW] 攔截請求:', event.request.url); | |
event.respondWith( | |
caches.match(event.request) | |
.then(function(response) { | |
if (response) { | |
console.log('[SW] 從快取回應:', event.request.url); | |
return response; | |
} | |
console.log('[SW] 從網路取得:', event.request.url); | |
return fetch(event.request); | |
}) | |
); | |
}); |