electron/docs/tutorial/updates.md
Tiago Danin 74769da9a7 [Docs] Fix URLs and Paths (#11584)
* Fix URLs and Paths in docs/

* Avoiding link break to /docs/development/updgrading-chrome.md

* Fix URLs and Paths in docs/ #2

* Removed double spaces in docs
2018-01-12 10:24:48 -05:00

3.8 KiB
Raw Blame History

Updating Applications

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

Deploying an update server

To get started, you first need to deploy a server that the autoUpdater module will download new updates from.

Depending on your needs, you can choose from one of these:

  • Hazel Update server for private or open-source apps. Can be deployed for free on Now (using a single command), pulls from GitHub Releases and leverages the power of GitHub's CDN.
  • Nuts Also uses GitHub Releases, but caches app updates on disk and supports private repositories.
  • electron-release-server Provides a dashboard for handling releases
  • Nucleus A complete update server for Electron apps maintained by Atlassian. Supports multiple applications and channels; uses a static file store to minify server cost.

If your app is packaged with electron-builder you can use the electron-updater module, which does not require a server and allows for updates from S3, GitHub or any other static file host.

Implementing updates in your app

Once you've deployed your update server, continue with importing the required modules in your code. The following code might vary for different server software, but it works like described when using Hazel.

Important: Please ensure that the code below will only be executed in your packaged app, and not in development. You can use electron-is-dev to check for the environment.

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

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

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

autoUpdater.setFeedURL(feed)

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.

Applying updates

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.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, (response) => {
    if (response === 0) autoUpdater.quitAndInstall()
  })
})

Also make sure that errors are being handled. Here's an example for logging them to stderr:

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