Updating Applications更新应用程序
There are several ways to provide automatic updates to your Electron application. 有几种方法可以为Electron应用程序提供自动更新。The easiest and officially supported one is taking advantage of the built-in Squirrel framework and Electron's autoUpdater module.最简单且得到官方支持的是利用内置的Squirrel框架和Electron的autoUpdater模块。
Using 使用update.electronjs.org
The Electron team maintains update.electronjs.org, a free and open-source webservice that Electron apps can use to self-update. Electron团队维护update.electronjs.org,这是一个免费的开源Web服务,Electron应用程序可以使用它进行自我更新。The service is designed for Electron apps that meet the following criteria:该服务专为满足以下标准的Electron应用程序设计:
App runs on macOS or Windows应用程序在macOS或Windows上运行App has a public GitHub repository应用程序具有公共GitHub存储库Builds are published to GitHub Releases生成发布到GitHub版本Builds are code-signed生成是代码签名的
The easiest way to use this service is by installing update-electron-app, a Node.js module preconfigured for use with update.electronjs.org.使用此服务的最简单方法是安装update-electron-app,这是一个预配置用于update.electronjs.org的Node.js模块。
Install the module using your Node.js package manager of choice:使用您选择的Node.js包管理器安装模块:
- npm
- Yarn
npm install update-electron-app
yarn add update-electron-app
Then, invoke the updater from your app's main process file:然后,从应用程序的主进程文件调用更新程序:
require('update-electron-app')()
By default, this module will check for updates at app startup, then every ten minutes. 默认情况下,此模块将在应用程序启动时检查更新,然后每十分钟检查一次。When an update is found, it will automatically be downloaded in the background. 找到更新后,将在后台自动下载。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.如果需要自定义配置,可以传递选项来更新Electron应用程序或直接使用更新服务。
Using other update services使用其他更新服务
If you're developing a private Electron application, or if you're not publishing releases to GitHub Releases, it may be necessary to run your own update server.如果您正在开发一个专用Electron应用程序,或者如果您没有向GitHub releases发布版本,则可能需要运行您自己的更新服务器。
Step 1: Deploying an update server部署更新服务器
Depending on your needs, you can choose from one of these:根据您的需要,您可以选择以下选项之一:
- Hazel –
Update server for private or open-source apps which can be deployed for free on Vercel.为可在Vercel上免费部署的私有或开源应用程序更新服务器。It pulls from GitHub Releases and leverages the power of GitHub's CDN.它借鉴了GitHub版本,并利用了GitHub CDN的强大功能。 - Nuts –
Also uses GitHub Releases, but caches app updates on disk and supports private repositories.也使用GitHub版本,但在磁盘上缓存应用程序更新并支持私有存储库。 - electron-release-server –
Provides a dashboard for handling releases and does not require releases to originate on GitHub.提供用于处理发布的仪表板,不要求发布源于GitHub。 - Nucleus –
A complete update server for Electron apps maintained by Atlassian.Atlassian维护的Electron应用程序的完整更新服务器。Supports multiple applications and channels; uses a static file store to minify server cost.支持多种应用和渠道;使用静态文件存储来降低服务器成本。
Once you've deployed your update server, you can instrument your app code to receive and apply the updates with Electron's [autoUpdater] module.部署完更新服务器后,您可以使用Electron的[autoUpdater]模块检测应用程序代码以接收和应用更新。
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.以下代码可能因不同的服务器软件而异,但其工作原理与使用Hazel时所描述的类似。
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.您可以使用app.isPackaged API检查环境。
const { app, autoUpdater, dialog } = require('electron')
Next, construct the URL of the update server feed and tell autoUpdater about it:接下来,构造更新服务器提要的URL并将其告知autoUpdater:
const server = 'https://your-deployment-url.com'
const url = `${server}/update/${process.platform}/${app.getVersion()}`
autoUpdater.setFeedURL({ url })
As the final step, check for updates. 最后一步,检查更新。The example below will check every minute:下面的示例将每分钟检查一次:
setInterval(() => {
autoUpdater.checkForUpdates()
}, 60000)
Once your application is packaged, it will receive an update for each new GitHub Release that you publish.应用程序打包后,它将收到您发布的每个新GitHub版本的更新。
Step 3: Notifying users when updates are available更新可用时通知用户
Now that you've configured the basic update mechanism for your application, you need to ensure that the user will get notified when there's an update. 既然您已经为应用程序配置了基本的更新机制,那么您需要确保在发生更新时通知用户。This can be achieved using the autoUpdater API events:这可以通过autoUpdater API事件实现:
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. 还要确保正在处理错误。Here's an example for logging them to 以下是将它们记录到stderr
:stderr
的示例:
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). 由于autoUpdate发出的请求不在您的直接控制之下,您可能会发现难以处理的情况(例如,如果更新服务器落后于身份验证)。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. url
字段支持file://
协议,这意味着通过一些努力,您可以通过从本地目录加载更新来避开进程的服务器通信方面。Here's an example of how this could work.这是一个如何工作的例子。