跳转至正文

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 bridge)

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() 在渲染进程早期执行,以确保回滚保护按预期工作。
  • 保持内置路径稳定,由 updater 决定是加载内置 bundle 还是下载的 bundle。
  • 当 Electron 应用共享后端发布 pipeline 时,可复用移动端已有的 Capgo channel 和 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