Programmatically update service worker if update()

2019-07-24 03:14发布

How can I programmatically update service worker since ServiceWorkerRegistration.update() has not been implemented in Chrome yet? Is there an alternative?

3条回答
萌系小妹纸
2楼-- · 2019-07-24 03:37

Thr best workaround seems to be to force update by changing checksum of the service worker file by some simple backend code (it can be like last commented line with microtime), which will be detected by browser

查看更多
贼婆χ
3楼-- · 2019-07-24 03:46

I assume you got this by now but I think you want serviceWorker.skipWaiting()

查看更多
forever°为你锁心
4楼-- · 2019-07-24 03:49

From the spec.

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw-test/sw.js', {scope: 'sw-test'}).then(function(registration) {
    // registration worked
    console.log('Registration succeeded.');
    button.onclick = function() {
      registration.update();
    }
  }).catch(function(error) {
    // registration failed
    console.log('Registration failed with ' + error);
  });
};
查看更多
登录 后发表回答