const CACHE_NAME = 'getid-cache-v1'; const URL_TO_CACHE = ['/getid']; self.addEventListener('install', (event) => { event.waitUntil( caches.open(CACHE_NAME).then((cache) => { return cache.addAll(URL_TO_CACHE); }) ); self.skipWaiting(); }); self.addEventListener('fetch', (event) => { if (event.request.url.endsWith('/getid')) { // オンラインなら何もしない(ブラウザのfetchが動く) if (!self.navigator || !self.navigator.onLine) { event.respondWith( caches.match(event.request).then((response) => { return response || new Response('[]', { headers: { 'Content-Type': 'application/json' } }); }) ); } } });