updated cache stuff

This commit is contained in:
Martin Donnelly 2017-12-15 20:48:11 +00:00
parent 71da4e4b6f
commit f7ab09f3c7
2 changed files with 24 additions and 10 deletions

View File

@ -31,12 +31,12 @@ const Minibus = require('minibus');
} }
}; };
/*if ('serviceWorker' in navigator) if ('serviceWorker' in navigator)
navigator.serviceWorker navigator.serviceWorker
.register('./service-worker.js') .register('./service-worker.js')
.then(function() { .then(function() {
console.log('Service Worker Registered'); console.log('Service Worker Registered');
});*/ });
app.createViews(); app.createViews();

View File

@ -12,13 +12,21 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
var dataCacheName = 'traintimesData-v1'; const dataCacheName = 'traintimesData-v1';
var cacheName = 'traintimePWA-final-1'; const cacheName = 'traintimePWA-final-1';
var filesToCache = [ const filesToCache = [
'/', '/',
'/index.html', '/index.html',
'/service-worker.js',
'/manifest.json',
'/browserconfig.xml',
'/css/common.css',
'/css/mui.custom.css',
'/fonts/fonts.css',
'/fonts/Roboto_Condensed-normal-400.woff',
'/fonts/Roboto_Slab-normal-400.woff',
'/js/bundle.js', '/js/bundle.js',
'/css/common.css' '/js/vendor.js'
]; ];
self.addEventListener('install', function(e) { self.addEventListener('install', function(e) {
@ -26,6 +34,7 @@ self.addEventListener('install', function(e) {
e.waitUntil( e.waitUntil(
caches.open(cacheName).then(function(cache) { caches.open(cacheName).then(function(cache) {
console.log('[ServiceWorker] Caching app shell'); console.log('[ServiceWorker] Caching app shell');
return cache.addAll(filesToCache); return cache.addAll(filesToCache);
}) })
); );
@ -38,11 +47,13 @@ self.addEventListener('activate', function(e) {
return Promise.all(keyList.map(function(key) { return Promise.all(keyList.map(function(key) {
if (key !== cacheName && key !== dataCacheName) { if (key !== cacheName && key !== dataCacheName) {
console.log('[ServiceWorker] Removing old cache', key); console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key); return caches.delete(key);
} }
})); }));
}) })
); );
/* /*
* Fixes a corner case in which the app wasn't returning the latest data. * Fixes a corner case in which the app wasn't returning the latest data.
* You can reproduce the corner case by commenting out the line below and * You can reproduce the corner case by commenting out the line below and
@ -58,9 +69,10 @@ self.addEventListener('activate', function(e) {
self.addEventListener('fetch', function(e) { self.addEventListener('fetch', function(e) {
console.log('[Service Worker] Fetch', e.request.url); console.log('[Service Worker] Fetch', e.request.url);
var dataUrl = '/getnexttraintimes?'; const dataUrl = '/getnexttraintimes?';
if (e.request.url.indexOf(dataUrl) > -1) { if (e.request.url.indexOf(dataUrl) > -1) {
console.log('!'); console.log('!');
/* /*
* When the request URL contains dataUrl, the app is asking for fresh * When the request URL contains dataUrl, the app is asking for fresh
* weather data. In this case, the service worker always goes to the * weather data. In this case, the service worker always goes to the
@ -70,13 +82,16 @@ self.addEventListener('fetch', function(e) {
*/ */
e.respondWith( e.respondWith(
caches.open(dataCacheName).then(function(cache) { caches.open(dataCacheName).then(function(cache) {
return fetch(e.request).then(function(response){ return fetch(e.request).then(function(response) {
cache.put(e.request.url, response.clone()); cache.put(e.request.url, response.clone());
return response; return response;
}); });
}) })
); );
} else { }
else
/* /*
* The app is asking for app shell files. In this scenario the app uses the * The app is asking for app shell files. In this scenario the app uses the
* "Cache, falling back to the network" offline strategy: * "Cache, falling back to the network" offline strategy:
@ -87,5 +102,4 @@ self.addEventListener('fetch', function(e) {
return response || fetch(e.request); return response || fetch(e.request);
}) })
); );
}
}); });