Lewis
Lewis

Reputation: 14926

Programmatically update service worker if update() is not available

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

Upvotes: 4

Views: 4057

Answers (3)

Sten Muchow
Sten Muchow

Reputation: 6711

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);
  });
};

Upvotes: 1

David
David

Reputation: 3001

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

Upvotes: 6

Karol Klepacki
Karol Klepacki

Reputation: 2118

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

Upvotes: 0

Related Questions