Updated caching

This commit is contained in:
Martin Donnelly 2018-09-03 17:52:43 +01:00
parent 9f36c2b163
commit d12188413a

View File

@ -99,11 +99,12 @@ self.addEventListener('activate', event => {
self.addEventListener('fetch', event => {
console.warn('Fetch', event.request.url);
// Skip cross-origin requests, like those for Google Analytics.
if (event.request.url.startsWith(self.location.origin)) {
/* if (event.request.url.startsWith(self.location.origin)) {
console.log('One of our requests..');
event.respondWith(
caches.match(event.request).then(cachedResponse => {
if (cachedResponse) {
console.log('cachedResponse', cachedResponse);
return cachedResponse;
}
@ -111,12 +112,32 @@ self.addEventListener('fetch', event => {
return fetch(event.request).then(response => {
// Put a copy of the response in the runtime cache.
return cache.put(event.request, response.clone()).then(() => {
console.log('fetch cache response', response);
return response;
});
});
});
})
);
}*/
if (event.request.url.startsWith(self.location.origin)) {
console.log('One of our requests..');
event.respondWith(
caches.open(RUNTIME).then(function(cache) {
return cache.match(event.request).then(function(response) {
var fetchPromise = fetch(event.request).then(function(networkResponse) {
console.log('Updating cache:', event.request);
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
return response || fetchPromise;
});
})
);
}
if (event.request.url.startsWith('https://maps.googleapis.com')) {
@ -124,9 +145,8 @@ self.addEventListener('fetch', event => {
const locCache = `loc-${url.searchParams.get('latlng')}`;
event.respondWith(
caches.match(locCache).then(cachedResponse => {
if (cachedResponse) {
if (cachedResponse)
return cachedResponse;
}
return caches.open(RUNTIME).then(cache => {
return fetch(event.request).then(response => {