跳转至正文

Electron 更新器 Electron 插件

为 Electron 应用提供 OTA Live Updates,API 接口与 capacitor-updater 保持一致

安装

bun add @capgo/electron-updater
npm、pnpm 或 yarn
  • npm install @capgo/electron-updater
  • pnpm add @capgo/electron-updater
  • yarn add @capgo/electron-updater

指南

如何在 Electron 中使用 Electron Updater

使用 @capgo/electron-updater

@capgo/electron-updater 让 Electron 应用拥有与 Capgo 相同的 Live Update 模型 @capgo/capacitor-updater。在主进程中初始化它,通过 preload 暴露 renderer bridge,然后调用 notifyAppReady() 在每次启动时执行,以便回滚保护机制确认 bundle 状态正常。

安装

bun add @capgo/electron-updater

主进程配置

import { app, BrowserWindow } from 'electron';
import path from 'node:path';
import { ElectronUpdater, setupEventForwarding, setupIPCHandlers } from '@capgo/electron-updater';

const updater = new ElectronUpdater({
  appId: 'com.example.desktop',
  autoUpdate: true,
});

app.whenReady().then(async () => {
  const window = new BrowserWindow({
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      contextIsolation: true,
    },
  });

  const builtinPath = path.join(__dirname, 'www/index.html');
  await updater.initialize(window, builtinPath);
  setupIPCHandlers(updater);
  setupEventForwarding(updater, window);

  await window.loadFile(updater.getCurrentBundlePath());
});

Preload 桥接

import { exposeUpdaterAPI } from '@capgo/electron-updater/preload';

exposeUpdaterAPI();

渲染进程用法

import { requireUpdater } from '@capgo/electron-updater/renderer';

const updater = requireUpdater();

await updater.notifyAppReady();

const latest = await updater.getLatest();

if (latest.url && !latest.error) {
  const bundle = await updater.download({
    url: latest.url,
    version: latest.version,
    checksum: latest.checksum,
  });

  await updater.next({ id: bundle.id });
}

监听更新事件

updater.addListener('download', ({ percent }) => {
  console.log('Download progress', percent);
});

updater.addListener('updateFailed', ({ bundle }) => {
  console.error('Update failed', bundle.version);
});

部署新 bundle

bun run build
bunx @capgo/cli@latest bundle upload --channel=production

实战建议

  • 务必调用 notifyAppReady() 在 renderer 早期执行,以确保回滚保护机制按预期工作。
  • 保持内置路径稳定,让 updater 决定加载已发布的 bundle 还是下载的 bundle。
  • 当您的 Electron 应用与移动端共享后端发布 pipeline 时,请复用您在移动端已使用的相同 Capgo 通道和 rollout 模型。

从“使用 @capgo/electron-updater”继续

如果你正在使用 使用 @capgo/electron-updater 要规划原生插件工作,请将其与 @capgo/electron-updater 有关 @capgo/electron-updater 中的实现细节, Electron Updater 入门指南 有关《Electron Updater 入门》中的实现细节, Capgo 插件目录 查看 Capgo 插件目录中的产品工作流, Capgo 提供的 Capacitor 插件 有关 Capgo 开发的 Capacitor 插件的实现细节,以及 添加或更新插件 有关添加或更新插件的实现细节。

常见问题

Electron Updater 插件常见问题

如何在 Electron 应用中安装 Electron Updater 插件?

运行 "bun add @capgo/electron-updater"(或 "npm install @capgo/electron-updater"),然后按照指南中的 Electron 设置步骤以及 GitHub README 进行操作。

@capgo/electron-updater 支持 React、Vue 和 Angular 吗?

支持。Electron Updater 是一个 Electron 包,因此适用于任何在 Electron 中运行的 Web 框架,包括 React、Vue、Angular、Svelte 以及原生 JavaScript。

@capgo/electron-updater 是免费且开源的吗?

是的。code 的源代码在 GitHub 上公开,地址为 https://github.com/Cap-go/electron-updater/,并且可以从 npm 免费安装。欢迎提交 Bug 报告和 Pull Request。

无需等待应用审核,即可发布 Electron Updater 变更

只要插件已包含在你的商店版本中,Capgo live updates 就能在几分钟内把 JavaScript、HTML 和 CSS 的改动推送给用户。

开始使用 Capgo