指南
如何在 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 插件的实现细节,以及 添加或更新插件 有关添加或更新插件的实现细节。