lib/generators/serviceworker/templates/serviceworker.js in serviceworker-rails-0.5.1 vs lib/generators/serviceworker/templates/serviceworker.js in serviceworker-rails-0.5.2

- old
+ new

@@ -1,53 +1,64 @@ -// function onInstall(event) { -// console.log('[Serviceworker]', "Installing!", event); -// event.waitUntil( -// caches.open('cached-assets-v1').then(function prefill(cache) { -// return cache.addAll([ -// '<%%= asset_path "application.js" %>', -// '<%%= asset_path "application.css" %>', -// '/offline.html' -// ]); -// }) -// ); -// } -// -// function onActivate(event) { -// console.log('[Serviceworker]', "Activating!", event); -// event.waitUntil( -// caches.keys().then(function(cacheNames) { -// return Promise.all( -// cacheNames.filter(function(cacheName) { -// // Return true if you want to remove this cache, -// // but remember that caches are shared across -// // the whole origin -// return key.indexOf('v1') !== 0; -// }).map(function(cacheName) { -// return caches.delete(cacheName); -// }) -// ); -// }) -// ); -// } -// -// function onFetch(event) { -// // Fetch from network, fallback to cached content, then offline.html for same-origin GET requests -// var request = event.request; -// -// if (!request.url.match(/^https?:\/\/example.com/) ) { return; } -// if (request.method !== 'GET') { return; } -// -// event.respondWith( -// fetch(request). // first, the network -// .catch(function fallback() { -// caches.match(request).then(function(response) { // then, the cache -// response || caches.match("/offline.html"); // then, /offline cache -// }) -// }) -// ); -// -// // See https://jakearchibald.com/2014/offline-cookbook/#on-network-response for more examples -// } -// -// self.addEventListener('install', onInstall); -// self.addEventListener('activate', onActivate); -// self.addEventListener('fetch', onFetch); +var CACHE_VERSION = 'v1' +var CACHE_NAME = 'sw-cache-' + CACHE_VERSION; + +function onInstall(event) { + console.log('[Serviceworker]', "Installing!", event); + event.waitUntil( + caches.open(CACHE_NAME).then(function prefill(cache) { + return cache.addAll([ + + // make sure serviceworker.js is not required by application.js + // if you want to reference application.js from here + '<%%#= asset_path "application.js" %>', + + '<%%= asset_path "application.css" %>', + + '/offline.html', + + ]); + }) + ); +} + +function onActivate(event) { + console.log('[Serviceworker]', "Activating!", event); + event.waitUntil( + caches.keys().then(function(cacheNames) { + return Promise.all( + cacheNames.filter(function(cacheName) { + // Return true if you want to remove this cache, + // but remember that caches are shared across + // the whole origin + return key.indexOf(CACHE_VERSION) !== 0; + }).map(function(cacheName) { + return caches.delete(cacheName); + }) + ); + }) + ); +} + +// Borrowed from https://github.com/TalAter/UpUp +function onFetch(event) { + event.respondWith( + // try to return untouched request from network first + fetch(event.request).catch(function() { + // if it fails, try to return request from the cache + return caches.match(event.request).then(function(response) { + if (response) { + return response; + } + // if not found in cache, return default offline content for navigate requests + if (event.request.mode === 'navigate' || + (event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html'))) { + console.log('[Serviceworker]', "Fetching offline content", event); + return caches.match('/offline.html'); + } + }) + }) + ); +} + +self.addEventListener('install', onInstall); +self.addEventListener('activate', onActivate); +self.addEventListener('fetch', onFetch);