I follow the following html5 rocks guide for service workers ( http://www.html5rocks.com/en/tutorials/service-worker/introduction/#disqus_thread ) and I encounter a problem that my service worker does not want to update.
I have a file called "app.js" with the following code:
navigator.serviceWorker.register('worker.js').then(function(reg) {
console.log('◕‿◕', reg);
}, function(err) {
console.log('ಠ_ಠ', err);
});
I also have a file called "worker.js" with the following code:
console.log("SW startup");
var myCache = 'myapp-static-v3';
var urlsToCache = [
'/',
'/assets/style.css',
'app.js'
];
self.addEventListener('install', function(event) {
console.log("SW installed");
event.waitUntil(
caches.open(myCache).then(function(cache) {
console.log("opened cache");
return cache.addAll(urlsToCache);
})
)
});
self.addEventListener('activate', function(event) {
var cacheWhitelist = [myCache];
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request)
.then(function(response) {
if (response) {
return response;
}
var fetchRequest = event.request.clone();
return fetch(fetchRequest).then(
function(response) {
if(!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
var responseToCache = response.clone();
caches.open(CACHE_NAME)
.then(function(cache) {
cache.put(event.request, responseToCache);
});
return response;
}
);
})
);
});
I tried updating the actual variable name myCache by switching the style.css file to another file and actually changing the contents.
Does anyone know what I'm doing wrong?
source
share