I have a fairly simple service worker (see code below), with which I run into the following problem:
On each refresh the browser tries to install a new service worker (even though there are no changes in the service worker nor my cached resources). This gets my service worker blocked in a constant "installing" phase after a first refresh of my site (see Screenshot). This happens independently, if I use skipWaiting() or not.
Everything else looks fine: No error messages on registration, all resources can be found in "Cache Storage" etc.
EDIT: So I have found that a call of history.pushState() on initialization of our app is blocking my service worker in "installing" state. If I run the app without this call or with a setTimeout(), the service worker runs fine. This gives me the impression that a call of history.pushState() BEFORE service worker registration makes the worker block. Any ideas why?
var version = 1489760334594;
var CACHE_NAME = 'mini-cache-v' + version;
var urlsToCache = ['./index.html','./app.css','./app.js','./assets/layers-2x.png','./assets/layers.png','./assets/logo-x48.png','./assets/logo.png','./assets/marker-icon.png','./assets/marker-shadow.png','./assets/poi.png','./assets/status.png','./assets/MaterialIcons-Regular.eot','./assets/MaterialIcons-Regular.ttf','./assets/roboto-regular.ttf','./assets/MaterialIcons-Regular.woff','./assets/MaterialIcons-Regular.woff2'];
self.addEventListener('install', function (event) {
console.log('INSTALL SW: ', CACHE_NAME);
//Open cache and add new resources from network
event.waitUntil(
caches.open(CACHE_NAME)
.then(function (cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache)
.then(function(){
console.log('Added resources');
})
})
);
//Activate new SW directly
// console.log('SKIP WAITING');
// return self.skipWaiting();
});
self.addEventListener('activate', function(event) {
console.log('ACTIVATE SW: ', CACHE_NAME);
//Delete old caches
event.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(keyList.map(function(key) {
if (key !== CACHE_NAME) {
return caches.delete(key);
}
}));
})
);
});
self.addEventListener('fetch', function(event) {
//If present serve from cache, else from network
event.respondWith(
caches.open(CACHE_NAME).then(function(cache){
return cache.match(event.request.url)
.then(function(response) {
if (response) {
console.log('SERVED FROM CACHE:', response);
return response;
}
return fetch(event.request).then(function(response){
// console.log('Response from network is:', response);
return response;
});
}
)
})
);
});