javascript - Service Worker is also Being cached in Chrome? - Stack Overflow

I built a Progressive Web App, .I don't understand why my service worker is also being cached in

I built a Progressive Web App, . I don't understand why my service worker is also being cached in Chrome? .js So when I change the service-worker, the chrome doesn't detect the change thus the service worker is not updating.

Eventough I refresh the page many times, it's still the same. However, in Mozilla it works just fine. Here's my code for installing the service worker

if ('serviceWorker' in navigator && (window.location.protocol === 'https:')) {
      navigator.serviceWorker.register('/service-worker-tavest.js')
      .then(function(registration) {
        // updatefound is fired if service-worker.js changes.
        registration.onupdatefound = function() {
          // updatefound is also fired the very first time the SW is installed,
          // and there's no need to prompt for a reload at that point.
          // So check here to see if the page is already controlled,
          // i.e. whether there's an existing service worker.
          if (navigator.serviceWorker.controller) {
            // The updatefound event implies that registration.installing is set:
            // .html#service-worker-container-updatefound-event
            var installingWorker = registration.installing;

            installingWorker.onstatechange = function() {
              switch (installingWorker.state) {
                case 'installed':
                  // At this point, the old content will have been purged and the
                  // fresh content will have been added to the cache.
                  // It's the perfect time to display a "New content is
                  // available; please refresh." message in the page's interface.
                  console.warn('New content is available, please refresh the page');
                  
                  break;

                case 'redundant':
                  throw new Error('The installing ' +
                                  'service worker became redundant.');

                default:
                  // Ignore
              }
            };
          }
        };
      }).catch(function(e) {
        console.error('Error during service worker registration:', e);
      });
    }

I built a Progressive Web App, https://www.tavest.. I don't understand why my service worker is also being cached in Chrome? https://www.tavest./service-worker-tavest.js So when I change the service-worker, the chrome doesn't detect the change thus the service worker is not updating.

Eventough I refresh the page many times, it's still the same. However, in Mozilla it works just fine. Here's my code for installing the service worker

if ('serviceWorker' in navigator && (window.location.protocol === 'https:')) {
      navigator.serviceWorker.register('/service-worker-tavest.js')
      .then(function(registration) {
        // updatefound is fired if service-worker.js changes.
        registration.onupdatefound = function() {
          // updatefound is also fired the very first time the SW is installed,
          // and there's no need to prompt for a reload at that point.
          // So check here to see if the page is already controlled,
          // i.e. whether there's an existing service worker.
          if (navigator.serviceWorker.controller) {
            // The updatefound event implies that registration.installing is set:
            // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-container-updatefound-event
            var installingWorker = registration.installing;

            installingWorker.onstatechange = function() {
              switch (installingWorker.state) {
                case 'installed':
                  // At this point, the old content will have been purged and the
                  // fresh content will have been added to the cache.
                  // It's the perfect time to display a "New content is
                  // available; please refresh." message in the page's interface.
                  console.warn('New content is available, please refresh the page');
                  
                  break;

                case 'redundant':
                  throw new Error('The installing ' +
                                  'service worker became redundant.');

                default:
                  // Ignore
              }
            };
          }
        };
      }).catch(function(e) {
        console.error('Error during service worker registration:', e);
      });
    }

Thank you for your help

Warm regards,

Share Improve this question asked Feb 6, 2017 at 2:29 Albert SocijoAlbert Socijo 611 silver badge6 bronze badges 1
  • Possible duplicate of Service worker JavaScript update frequency (every 24 hours?) – Jeff Posnick Commented Feb 6, 2017 at 15:30
Add a ment  | 

2 Answers 2

Reset to default 5

It's because the service worker is just a normal JS file and it will be browser-cached.

You can set no-cache header to the /service-worker-tavest.js so that the browser will stop caching your service worker file. That way you can have your service worker updated immediately when the new file is uploaded.

Here is how to do that in Nginx:

location /service-worker-tavest.js {
  # Don't use browser cache for service worker file
  add_header cache-control "no-store, no-cache, must-revalidate";
}

I think I know the answer regarding to this caching. It is because of "Service worker Lifecycle" in Chrome.

https://www.youtube./watch?v=TF4AB75PyIc

Conclusion: The cache in chrome browser is okay because chrome will update the service worker by itself.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745340881a4623320.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信