跳转到主内容

更新应用程序

有若干种方法可以自动更新您的 Electron 应用程序。 最简单并且获得官方支持的方法是利用内置的Squirrel框架和Electron的autoUpdater模块。

使用 update.electronjs.org

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

  • 应用运行在 macOS 或者 Windows
  • 应用有公开的 GitHub 仓库
  • 构建需要发布到 GitHub Releases
  • 构建是经过代码签名

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

使用您选择的 Node.js 包管理器安装模块:

npm install update-electron-app

然后,从应用的主进程文件中调用更新模块:

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

默认情况下,这个模块会在应用启动的时候检查更新,然后每隔十分钟再检查一次。 当发现了一个更新,它会自动在后台下载。 当下载完成后,会显示对话框允许用户重启应用。

如果你需要定制化你的配置,你可以 将配置设置传递给 update-electron-app 或者 直接使用更新服务

使用其他更新服务

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

第一步:部署更新服务器

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

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

一旦您部署了更新服务器,您就可以编写您的应用代码,以使用 Electron 的 autoUpdater 模块接收和应用更新。

第二步:在你的应用程序上接收更新

首先,在您的主进程代码中导入所需模块。 The following code might vary for different server software, but it works like described when using Hazel.

注意检查执行环境!

请确保以下代码仅在打包的应用程序执行,而不是在开发环境中。 您可以使用 app.isPackaged API 来检查环境。

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

接下来,构建更新服务器的 URL 并通知 autoUpdater

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

autoUpdater.setFeedURL({ url })

最后一步,检查更新。 下面的示例将在每分钟检查一次:

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

Once your application is packaged, it will receive an update for each new GitHub Release that you publish.

第三步:当更新可用时通知用户

现在您已经为应用程序配置了基本的更新机制, 您需要确保在更新时通知用户. 这可以使用 autoUpdater API 事件实现:

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. Starta om applikationen för att verkställa uppdateringarna.'
}

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

另外,也请确认错误被处理。 下面是将错误日志输出到stderr的例子。

main.js
autoUpdater.on('error', (message) => {
console.error('There was a problem updating the application')
console.error(message)
})
手动处理更新

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.