UNPKG

react-raika

Version:

react-starter

63 lines (59 loc) 1.64 kB
const CACHE_NAME = 'my-cache-v1'; const urlsToCache = [ '/', '/styles/main.css', '/script/main.js' ]; self.addEventListener('install', function(event) { console.log('Installing service worker', event); event.waitUntil( caches.open(CACHE_NAME) .then(function(cache) { console.log('Opened cache'); return cache.addAll(urlsToCache); }) ); // ...existing code... }); self.addEventListener('activate', function(event) { console.log('Activating service worker', event); // ...existing code... event.waitUntil( caches.keys().then(function(cacheNames) { return Promise.all( cacheNames.map(function(cacheName) { if (cacheName !== CACHE_NAME) { console.log('Deleting old cache:', cacheName); return caches.delete(cacheName); } }) ); }) ); return self.clients.claim(); }); self.addEventListener('fetch', function(event) { console.log('Fetching something', event); event.respondWith( caches.match(event.request) .then(function(response) { if (response) { return response; } return fetch(event.request).then( function(response) { if (!response || response.status !== 200 || response.type !== 'basic') { return response; } const responseToCache = response.clone(); caches.open(CACHE_NAME) .then(function(cache) { cache.put(event.request, responseToCache); }); return response; } ); }) ); // ...existing code... });