メインコンテンツへ飛ぶ

autoUpdater

アプリを自動的に更新することができます。

プロセス: メイン

あなたのアプリケーションにどうやってアプリケーションの更新を実装するかの詳細なガイド もご覧ください。

autoUpdater は EventEmitter です。

プラットフォームに関する注意事項​

現在、macOS と Windows にのみ対応しています。 Linux では、自動更新の組み込みサポートがないので、アプリ更新にはディストリビューションのパッケージマネージャーの使用を推奨しています。

さらに、各プラットフォームではいくつかの微妙な違いがあります。

macOS​

macOSでは、autoUpdater モジュールは Squirrel.Mac で構築されているので、動作させるのに特別なセットアップ作業をする必要はありません。サーバー側の要件については、サーバーサポート をお読みください。注意として、App Transport Security (ATS) はアップデート処理の過程で行われたすべてのリクエストに適用されます。アプリのplistに NSAllowsArbitraryLoads キーを追加することで、ATSを無効にすることができます。

[!IMPORTANT] Your application must be signed for automatic updates on macOS. これは Squirrel.Mac の動作要件です。

The update ZIP is streamed to disk rather than held in memory. When the server answers with an ETag or Last-Modified header and honours Range requests, a download interrupted by a network change, sleep or the app quitting continues from where it stopped on the next update check, including after a relaunch; otherwise it starts over. An update entry (the update server's response, or an updateTo object in a serverType: 'json' feed) may declare sha256 (hex digest) and size (bytes) alongside url; a download that does not match them is discarded before it is unpacked and error is emitted.

An update entry may also carry delta, an object with from_version, url, sha256 and size, offering a binary patch from one earlier build. When from_version equals the running app's CFBundleVersion, that patch is downloaded instead of the ZIP, applied to a copy of the running app, and the result goes through the same code signing verification as an unpacked ZIP; on any failure the ZIP is downloaded in the same check. Patches are made with Sparkle's BinaryDelta create from the exact bundles that shipped; see Squirrel.Mac's README for the constraints.

Windows​

On Windows, the autoUpdater module automatically selects the appropriate update mechanism based on how your app is packaged:

You don't need to configure which updater to use; Electron automatically detects the packaging format and uses the appropriate one.

Squirrel.Windows​

Squirrel.Windows によって構築されたアプリは、適切なセットアップとアンインストールのため、カスタム起動イベント をトリガーします。Electron アプリケーションはこれを処理しなければなりません。

Squirrel.Windows アプリは、インストール後 --squirrel-firstrun 引数で自動的に起動されます。この間、Squirrel.Windows はアプリのファイルをロックし、autoUpdater はロックが解除されるまでリクエストに失敗します。実際、これにより初回起動から数秒間においてアップデートを確認できません。これには、process.argv の中に --squirrel-firstrun フラグが含まれている場合にアップデートを確認しないようにしたり、アップデートの確認に 10 秒の猶予を設けたりすることで対処できます。(詳細は electron/electron#7155 を参照してください。)

Squirrelで作成されたインストーラは、例えば、com.squirrel.slack.Slack や com.squirrel.code.Code といったcom.squirrel.PACKAGE_ID.YOUR_EXE_WITHOUT_DOT_EXE という形式による アプリケーションユーザーモデル ID を持つショートカットアイコンを作成します。 app.setAppUserModelId APIでアプリに対して同じIDを使うようにしてください。そうでないと、Windowsはタスクバーにアプリを正しくピン留めすることができません。

MSIX Packages​

When your app is packaged as an MSIX, the autoUpdater module provides additional functionality:

  • Use the allowAnyVersion option in setFeedURL() to allow updates to older versions (downgrades)
  • Support for direct MSIX file links or JSON update feeds (similar to Squirrel.Mac format)

イベント​

autoUpdater オブジェクトでは以下のイベントが発生します。

イベント: 'error'​

戻り値:

  • error Error

更新中にエラーがあるときに発生します。

イベント: 'checking-for-update'​

利用可能なアップデートの確認を開始した場合に発生します。

イベント: 'update-available'​

利用可能な更新がある場合に発生します。更新は自動ダウンロードされます。

イベント: 'update-not-available'​

利用可能な更新プログラムがないときに発生します。

イベント: 'update-downloaded'​

戻り値:

  • event Event
  • releaseNotes string
  • releaseName string
  • releaseDate Date
  • updateURL string

更新プログラムがダウンロードされたときに発生します。

With Squirrel.Windows only releaseName is available.

[!NOTE] このイベントを必ず処理する必要はありません。ダウンロードに成功した更新は、次回のアプリケーション起動時でも適用されます。

イベント: 'before-quit-for-update'​

History
Version(s)Changes
None
API ADDED

このイベントは、ユーザが呼び出した quitAndInstall() の後に発火されます。

この API が呼ばれた時、すべてのウィンドウが閉じられる前に before-quit イベントは発火されません。結果として、プロセス終了時にウィンドウが閉じられる前にアクションを実行するために、before-quit をリッスンする場合は、このイベントも同様にリッスンする必要があります。

メソッド​

autoUpdater オブジェクトには以下のメソッドがあります

autoUpdater.setFeedURL(options)​

History
Version(s)Changes
None

Added headers as a second parameter.

None

Changed API to accept a single options argument (contains url, headers, and serverType properties).

  • options Object
    • url string - The update server URL. For Windows MSIX, this can be either a direct link to an MSIX file (e.g., https://example.com/update.msix) or a JSON endpoint that returns update information (see the Squirrel.Mac README for more information).
    • headers Record<string, string> (任意) macOS - HTTP リクエストのヘッダ。
    • serverType string (任意) macOS - json、default のいずれかです。詳細は Squirrel.Mac README を参照してください。
    • allowAnyVersion boolean (optional) Windows - If true, allows downgrades to older versions for MSIX packages. 省略値は false です。

url を設定して自動更新を初期化します。

autoUpdater.getFeedURL()​

戻り値 string - 現在の更新フィード URL。

autoUpdater.checkForUpdates()​

更新があるかどうかサーバーに問い合わせます。この API を使用する前に setFeedURL を呼び出す必要があります。

[!NOTE] アップデート可能であれは、自動でダウンロードされます。 autoUpdater.checkForUpdates() を 2 回呼び出すと、更新データを 2 回ダウンロードすることになります。

autoUpdater.quitAndInstall()​

ダウンロード後にアプリを再起動し、更新をインストールします。 update-downloaded が発生した後でしか呼び出さないでください。

autoUpdater.quitAndInstall() を呼ぶと、この中では最初にすべてのアプリケーションウィンドウを閉じ、すべてのウィンドウが閉じられた後に自動的に app.quit() を呼び出します。

[!NOTE] アップデートを適用するために必ずこの関数を呼ぶ必要はなく、 ダウンロードに成功したアップデートは次回のアプリケーション起動時に必ず適用されます。