ServiceWorkerRegistration.update()

ServiceWorkerRegistrationupdate方法尝试更新service worker。获得worker脚本的URL,逐字节匹配新获取的worker和当前的worker,存在差异的时候安装新的worker。获取worker脚本的更新操作会忽略浏览器缓存的24小时前的内容。

注意: 这个特性也应用于 Web Workers.

语法

ServiceWorkerRegistration.update();

参数

None.

返回

返回 Promise 在resolve时对应一个 ServiceWorkerRegistration 对象。

示例

下面的示例注册一个service worker,然后绑定事件到按钮,这样你可以有需要时,明确的更新server worker:

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

标准

Specification Status Comment
Service Workers
ServiceWorkerRegistration.update()
Working Draft Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
update
Experimental
Chrome Full support 45
Notes
Full support 45
Notes
Notes Starting with Chrome 46, update() returns a promise that resolves with 'undefined' if the operation completed successfully or there was no update, and rejects if update failed. If the new worker ran but installation failed, the promise still resolves. Formerly, it raised an exception.
Notes Before Chrome 48, this method always bypassed the browser cache. Starting with Chrome 48, it only bypasses the cache when the previous service worker check was more than twenty-four hours ago.
Edge Full support 17
Full support 17
Full support 16
Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
Firefox Full support 44
Notes
Full support 44
Notes
Notes Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR).
IE No support NoOpera Full support 32
Notes
Full support 32
Notes
Notes Starting with Opera 33, update() returns a promise that resolves with 'undefined' if the operation completed successfully or there was no update, and rejects if update failed. If the new worker ran but installation failed, the promise still resolves. Formerly, it raised an exception.
Notes Before Opera 35, this method always bypassed the browser cache. Starting with Opera 35, it only bypasses the cache when the previous service worker check was more than twenty-four hours ago.
Safari Full support 11.1WebView Android Full support 45
Notes
Full support 45
Notes
Notes Starting with Chrome 46, update() returns a promise that resolves with 'undefined' if the operation completed successfully or there was no update, and rejects if update failed. If the new worker ran but installation failed, the promise still resolves. Formerly, it raised an exception.
Notes Before Chrome 48, this method always bypassed the browser cache. Starting with Chrome 48, it only bypasses the cache when the previous service worker check was more than twenty-four hours ago.
Chrome Android Full support 45
Notes
Full support 45
Notes
Notes Starting with Chrome 46, update() returns a promise that resolves with 'undefined' if the operation completed successfully or there was no update, and rejects if update failed. If the new worker ran but installation failed, the promise still resolves. Formerly, it raised an exception.
Notes Before Chrome 48, this method always bypassed the browser cache. Starting with Chrome 48, it only bypasses the cache when the previous service worker check was more than twenty-four hours ago.
Firefox Android Full support 44Opera Android Full support 32
Notes
Full support 32
Notes
Notes Starting with Opera 33, update() returns a promise that resolves with 'undefined' if the operation completed successfully or there was no update, and rejects if update failed. If the new worker ran but installation failed, the promise still resolves. Formerly, it raised an exception.
Notes Before Opera 35, this method always bypassed the browser cache. Starting with Opera 35, it only bypasses the cache when the previous service worker check was more than twenty-four hours ago.
Safari iOS No support NoSamsung Internet Android Full support 4.0

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

See also