Problem with Service Worker cache

Asked

Viewed 75 times

0

I have a service worker working perfectly. The problem is that it is caching the user out. So every time I update the page, even logged in, it shows the header out. My service worker:

importScripts("{% static 'js/cache-polyfill.js' %}");
  self.addEventListener('install', function(e) {
    e.waitUntil(
      caches.open('testv1').then(function(cache) {
        return cache.addAll([
          "/",
          "{% static 'assets/css/all.css' %}",
          "{% static 'assets/js/all.js' %}",
        ]);

      })
    );
  });
self.addEventListener('fetch', function(event) {
    event.respondWith(
      caches.match(event.request).then(function(response) {
        return response || fetch(event.request);
      })
    );
  });

1 answer

0

I found the solution. I needed to change my Eventlistener fetch according to my need: a home that updates frequently. (Reference)

self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.open('mysite-dynamic').then(function(cache) {
      return fetch(event.request).then(function(response) {
        cache.put(event.request, response.clone());
        return response;
      });
    })
  );
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.