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
.register('./service-worker.js')
.then(function() {
console.log('Service Worker Registered');
});*/
});
app.createViews();

View File

@ -12,13 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.
var dataCacheName = 'traintimesData-v1';
var cacheName = 'traintimePWA-final-1';
var filesToCache = [
const dataCacheName = 'traintimesData-v1';
const cacheName = 'traintimePWA-final-1';
const filesToCache = [
'/',
'/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',
'/css/common.css'
'/js/vendor.js'
];
self.addEventListener('install', function(e) {
@ -26,6 +34,7 @@ self.addEventListener('install', function(e) {
e.waitUntil(
caches.open(cacheName).then(function(cache) {
console.log('[ServiceWorker] Caching app shell');
return cache.addAll(filesToCache);
})
);
@ -38,11 +47,13 @@ self.addEventListener('activate', function(e) {
return Promise.all(keyList.map(function(key) {
if (key !== cacheName && key !== dataCacheName) {
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);
}
}));
})
);
/*
* 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
@ -58,9 +69,10 @@ self.addEventListener('activate', function(e) {
self.addEventListener('fetch', function(e) {
console.log('[Service Worker] Fetch', e.request.url);
var dataUrl = '/getnexttraintimes?';
const dataUrl = '/getnexttraintimes?';
if (e.request.url.indexOf(dataUrl) > -1) {
console.log('!');
/*
* When the request URL contains dataUrl, the app is asking for fresh
* weather data. In this case, the service worker always goes to the
@ -72,11 +84,14 @@ self.addEventListener('fetch', function(e) {
caches.open(dataCacheName).then(function(cache) {
return fetch(e.request).then(function(response) {
cache.put(e.request.url, response.clone());
return response;
});
})
);
} else {
}
else
/*
* The app is asking for app shell files. In this scenario the app uses the
* "Cache, falling back to the network" offline strategy:
@ -87,5 +102,4 @@ self.addEventListener('fetch', function(e) {
return response || fetch(e.request);
})
);
}
});