ServiceWorkerRegistration.update()

ServiceWorkerRegistration インターフェイスの update() メソッドは、サービスワーカーの更新を試みます。 ワーカーのスクリプト URL をフェッチし、新しいワーカーが現在のワーカーとバイト単位で同一でない場合は、新しいワーカーをインストールします。 前回のフェッチが24時間以上前に行われた場合、ワーカーのフェッチはブラウザーキャッシュをバイパスします。

: この機能は Web Worker で使用できます。

構文

serviceWorkerRegistration.update();

パラメーター

なし。

戻り値

ServiceWorkerRegistration オブジェクトで解決する Promise

次の簡単な例では、サービスワーカーの例を登録し、次にイベントハンドラーをボタンに追加して、必要に応じてサービスワーカーを明示的に更新できるようにします。

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw-test/sw.js', {scope: 'sw-test'}).then(function(registration) {
    // 登録しました
    console.log('登録に成功しました。');
    button.onclick = function() {
      registration.update();
    }
  }).catch(function(error) {
    // 登録に失敗しました
    console.log('登録に失敗しました:' + error);
  });
};

仕様

仕様 状態 コメント
Service Workers
ServiceWorkerRegistration.update() の定義
草案 初期定義

ブラウザーの互換性

Update compatibility data on GitHub
デスクトップモバイル
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewAndroid 版 ChromeAndroid 版 FirefoxAndroid 版 OperaiOSのSafariSamsung Internet
update
実験的
Chrome 完全対応 45
補足
完全対応 45
補足
補足 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.
補足 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 完全対応 17
完全対応 17
完全対応 16
無効
無効 From version 16: this feature is behind the Enable service workers preference.
Firefox 完全対応 44
補足
完全対応 44
補足
補足 Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 未対応 なしOpera 完全対応 32
補足
完全対応 32
補足
補足 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.
補足 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 完全対応 11.1WebView Android 完全対応 45
補足
完全対応 45
補足
補足 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.
補足 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 完全対応 45
補足
完全対応 45
補足
補足 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.
補足 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 完全対応 44Opera Android 完全対応 32
補足
完全対応 32
補足
補足 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.
補足 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 未対応 なしSamsung Internet Android 完全対応 4.0
補足
完全対応 4.0
補足
補足 Starting with Samsung Internet 5.0, 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.
補足 Before Samsung Internet 5.0, this method always bypassed the browser cache. Starting with Samsung Internet 5.0, it only bypasses the cache when the previous service worker check was more than twenty-four hours ago.

凡例

完全対応  
完全対応
未対応  
未対応
実験的。動作が変更される可能性があります。
実験的。動作が変更される可能性があります。
実装ノートを参照してください。
実装ノートを参照してください。
ユーザーが明示的にこの機能を有効にしなければなりません。
ユーザーが明示的にこの機能を有効にしなければなりません。

関連情報