JavaScript How and When should we write to cache in Service Workers? when should you write a functional (skills) résumé,when

  • Cache all requests from an app without explicitly specifying urlsToCache. So I will cache stuff under fetch event.
  • To respond to requests from the cache.
  • Update the cache when fetch is success.

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?

Answer:1

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 ...

  1. filter records based on a combo box selection
  2. access filter records based combobox selection
  3. filter form records based on combobox
  4. crystal reports filter records based on parameter
  5. how to filter records based on date in sql
  6. how to filter records based on date in oracle
  7. filter records in subform based on a combo box
  8. filter records from a datatable based on a condition

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 ...

  1. node mysql function
  2. sql server nodes function
  3. node js mysql query function
  4. node js call mysql function
  5. node mysql query function
  6. node js mysql function
  7. node js sync function mysql
  8. node-red function mysql

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 ...

  1. sequential animation with css
  2. sequential animation in android example
  3. sequential animation in qml
  4. sequential animation in powerpoint
  5. sequential animation in android