跳转到主内容

更新应用程序

There are several ways to provide automatic updates to your Electron application. The easiest and officially supported one is taking advantage of the built-in Squirrel framework and Electron's autoUpdater module.

Using update.electronjs.org

Electron 团队维护 update.electronjs.org,一个免费开源的网络服务,可以让 Electron 应用使用自动更新。 这个服务是设计给那些满足以下标准的 Electron 应用:

使用这个服务最简单的方法是安装 update-electron-app,一个预配置好的 Node.js 模块来使用 update.electronjs.org。

Install the module using your Node.js package manager of choice:

npm install update-electron-app

Then, invoke the updater from your app's main process file:

main.js
require('update-electron-app')()

默认情况下,这个模块会在应用启动的时候检查更新,然后每隔十分钟再检查一次。 当发现了一个更新,它会自动在后台下载。 When the download completes, a dialog is displayed allowing the user to restart the app.

If you need to customize your configuration, you can pass options to update-electron-app or use the update service directly.

Using other update services

如果你开发的是一个私有的 Electron 应用程序,或者你没有在 GitHub Releases 中公开发布,你可能需要运行自己的更新服务器。

Step 1: Deploying an update server

根据你的需要,你可以从下方选择:

  • Hazel – Update server for private or open-source apps which can be deployed for free on Vercel. 它从GitHub Releases中拉取更新文件,并且利用 GitHub CDN 的强大性能。
  • Nuts-同样使用GitHub Releases, 但得在磁盘上缓存应用程序更新并支持私有存储库.
  • electron-release-server – 提供一个用于处理发布的仪表板,并且不需要在GitHub上发布发布。
  • Nucleus – 一个由Atlassian维护的 Electron 应用程序的完整更新服务器。 支持多种应用程序和渠道; 使用静态文件存储来降低服务器成本.

Once you've deployed your update server, you can instrument your app code to receive and apply the updates with Electron's [autoUpdater] module.

Step 2: Receiving updates in your app

First, import the required modules in your main process code. The following code might vary for different server software, but it works like described when using Hazel.

Check your execution environment!

Please ensure that the code below will only be executed in your packaged app, and not in development. You can use the app.isPackaged API to check the environment.

main.js
const { app, autoUpdater, dialog } = require('electron')

Next, construct the URL of the update server feed and tell autoUpdater about it:

main.js
const server = 'https://your-deployment-url.com'
const url = `${server}/update/${process.platform}/${app.getVersion()}`

autoUpdater.setFeedURL({ url })

最后一步,检查更新。 The example below will check every minute:

main.js
setInterval(() => {
autoUpdater.checkForUpdates()
}, 60000)

应用程序被packaged后, 它将接收你每次发布在GitHub Release上的的更新。

Step 3: Notifying users when updates are available

现在您已经为应用程序配置了基本的更新机制, 您需要确保在更新时通知用户. This can be achieved using the autoUpdater API events:

main.js
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
const dialogOpts = {
type: 'info',
buttons: ['Restart', 'Later'],
title: 'Application Update',
message: process.platform === 'win32' ? releaseNotes : releaseName,
detail:
'A new version has been downloaded. Restart the application to apply the updates.',
}

dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) autoUpdater.quitAndInstall()
})
})

Also make sure that errors are being handled. 下面是将错误日志输出到stderr的例子。

main.js
autoUpdater.on('error', (message) => {
console.error('There was a problem updating the application')
console.error(message)
})
Handling updates manually

Because the requests made by autoUpdate aren't under your direct control, you may find situations that are difficult to handle (such as if the update server is behind authentication). The url field supports the file:// protocol, which means that with some effort, you can sidestep the server-communication aspect of the process by loading your update from a local directory. Here's an example of how this could work.