Spaces:
Running
on
Zero
Running
on
Zero
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'); | |
}) | |
); | |
}); |