urlsToCache
. So I will cache stuff under fetch
event.Initially,
this.addEventListener('fetch', function(event) {
var fetchReq = event.request.clone(),
cacheReq = event.request.clone();
event.respondWith(fetch(fetchReq).then(function(response) {
var resp = response.clone();
caches.open(CACHE_NAME).then(function(cache) {
req = event.request.clone();
cache.put(req, resp);
});
return response;
}).catch(function() {
return caches.match(cacheReq);
}));
});
The offline situations were handled perfectly well. But the problem here was with the slow connections. The user has to wait till fetch times out or throws an error to get the response from cache.
self.addEventListener('fetch', function(event) {
var cacheRequest = event.request.clone();
event.respondWith(caches.match(cacheRequest).then(function(response) {
if(response) return response;
var fetchRequest = event.request.clone();
return fetch(fetchRequest).then(function(response) {
var responseToCache = response.clone();
caches.open(cache_name).then(function(cache) {
var cacheSaveRequest = event.request.clone();
cache.put(cacheSaveRequest, responseToCache);
});
return response;
});
}));
});
With the cache taking precedence, the responses served were fine. But the problem here is that when the code updates. When /public/main.css
served via sw is updated, on page reload only the cache is served, the updated content is not served.
I also tried modifying the cache_name to cache-v2
from cache-v1
(so that sw binary diff exists and sw is updated and that old cache can be cleared), and cleared cache-v1
on activate
event. But it gave rise to new problems where two service workers were running at the same time under the same Registration ID
. More on this is in this other SO question: How to stop older service workers?
I am C++ programmer and new to JS. I am trying to filter table based on click event. I have following table. How can i keep records based on clicking keys and filtering other records. <!DOCTYPE ...
I am C++ programmer and new to JS. I am trying to filter table based on click event. I have following table. How can i keep records based on clicking keys and filtering other records. <!DOCTYPE ...
I want to get part of a path in URL via Angular.js and i found solution: http://mywebsite.com/one/HEREiWANT/three first i do this: app.config(function($locationProvider){ $locationProvider....
I want to get part of a path in URL via Angular.js and i found solution: http://mywebsite.com/one/HEREiWANT/three first i do this: app.config(function($locationProvider){ $locationProvider....
I am using mssql with node.js to connect to an sql server db. I am trying to reduce code by wrapping the connection code in a function with one query parameter. When I call the function from with in a ...
I am using mssql with node.js to connect to an sql server db. I am trying to reduce code by wrapping the connection code in a function with one query parameter. When I call the function from with in a ...
How can I make each of them to fadeInUp but in sequence? demo http://jsfiddle.net/uz2rm8jy/4/ HTML <div class="c one"></div> <div class="c two"></div> <div class="c ...
How can I make each of them to fadeInUp but in sequence? demo http://jsfiddle.net/uz2rm8jy/4/ HTML <div class="c one"></div> <div class="c two"></div> <div class="c ...