0Pricing
Electron Desktop App Development · 课时

自动更新 Electron 应用

借助 electron-updater、更新渠道和完善的更新流程,为用户提供顺畅的应用更新体验。

自动更新 Electron 应用 是 CoddyKit 上的免费 Electron Desktop App Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Electron Desktop App Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Electron Desktop App Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Auto-Update Matters

Desktop apps are not auto-refreshed like web pages. Once a user installs your Electron app, you need a way to push new versions without asking them to re-download manually.

An auto-update system lets you ship bug fixes and features continuously, keeping every user on a recent, secure build.

  • Reduces support burden from stale versions
  • Delivers security patches fast
  • Improves retention with new features

The electron-updater Library

The most common solution is electron-updater, part of the electron-builder ecosystem. It handles checking, downloading, and installing updates across Windows, macOS, and Linux.

Install it as a runtime dependency, not a dev dependency, because it ships inside your app.

npm install electron-updater

Configuring the Publish Target

electron-updater reads a publish block in your build config to know where to fetch new versions. A common target is a generic HTTP server or a GitHub release.

"build": {
  "publish": [{
    "provider": "generic",
    "url": "https://updates.example.com/myapp"
  }]
}

Triggering an Update Check

In your main process, call autoUpdater.checkForUpdates() after the app is ready. This contacts your publish URL and compares versions.

Run this from the main process only, never the renderer.

const { autoUpdater } = require('electron-updater')

app.whenReady().then(() => {
  createWindow()
  autoUpdater.checkForUpdates()
})

Listening to Update Events

autoUpdater emits events you can hook into to drive your UI and logging.

  • update-available — a newer version exists
  • download-progress — bytes transferred
  • update-downloaded — ready to install
autoUpdater.on('update-available', () => {
  console.log('New version found')
})

autoUpdater.on('download-progress', (p) => {
  console.log('Progress: ' + Math.round(p.percent) + '%')
})

Installing the Downloaded Update

When update-downloaded fires, the new version is staged. Call quitAndInstall() to restart the app onto the new version.

It is good practice to prompt the user first instead of restarting unexpectedly.

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

Update Channels

Channels let you ship pre-release builds to a subset of users. Common channels are latest, beta, and alpha.

Set the channel so testers receive early builds while everyone else stays on stable.

autoUpdater.channel = 'beta'
autoUpdater.allowPrerelease = true

Communicating Progress to the UI

Because autoUpdater lives in the main process, forward its events to the renderer through your preload bridge so the UI can show a progress bar.

autoUpdater.on('download-progress', (p) => {
  mainWindow.webContents.send('update-progress', p.percent)
})

Handling Errors Gracefully

Network failures and signature mismatches happen. Always listen for the error event so a failed update never crashes the app or blocks the user.

autoUpdater.on('error', (err) => {
  console.error('Update failed:', err == null ? 'unknown' : err.message)
})

Code Signing Requirements

On macOS and Windows, auto-updates only work if your app is code signed. Unsigned builds will fail the signature check on install.

  • macOS: Apple Developer ID certificate
  • Windows: Authenticode certificate

Signing also protects users from tampered update payloads.

Manual vs Silent Updates

You can disable automatic downloading and let users decide when to update by setting autoDownload = false, then calling downloadUpdate() on demand.

Choose silent background updates for consumer apps, manual control for enterprise tools.

autoUpdater.autoDownload = false

autoUpdater.on('update-available', () => {
  // ask user, then:
  autoUpdater.downloadUpdate()
})

Quick Check

Test your understanding of Electron auto-updates.

Recap

You learned how to ship seamless updates with electron-updater:

  • Configure a publish target
  • Call checkForUpdates() from the main process
  • React to update-available, download-progress, and update-downloaded
  • Use quitAndInstall() to apply
  • Code sign your builds and choose channels

A reliable update pipeline keeps your whole user base current and secure.

常见问题解答

「自动更新 Electron 应用」课时是免费的吗?

是的 — 「自动更新 Electron 应用」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Electron Desktop App Development 课程的其余内容,请升级到 CoddyKit PRO。 Electron Desktop App Development 课程共包含 4 节课。

「自动更新 Electron 应用」这节课中我会学到什么?

借助 electron-updater、更新渠道和完善的更新流程,为用户提供顺畅的应用更新体验。 你通过在浏览器中直接运行的动手代码来练习 Electron Desktop App Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Electron Desktop App Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Electron Desktop App Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「自动更新 Electron 应用」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Electron Desktop App Development 课中编写并运行代码吗?

能。每节 Electron Desktop App Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 多窗口架构
  2. 后台进程与工作线程
  3. 与云服务集成
  4. 自动更新 Electron 应用
← 返回 Electron Desktop App Development