Skip to main content
プラグインに戻る
@capgo/electron-updater
チュートリアル
githubのチュートリアル

エレクトロン アップデート

OTA live updates for Electron apps with the same API surface as capacitor-updater

ガイド

Electron アップデーターに関するチュートリアル

Electron アプリにライブアップデート機能を提供するために、@capgo/electron-updater を使用します。

@capgo/electron-updater Electron アプリにライブアップデート機能を提供するために、@Capgo/electron-updater を使用します。 @capgo/capacitor-updatermain プロセスで初期化し、プリロードでレンダラー ブリッジを公開し、 notifyAppReady() 毎回起動することで、ロールバック保護がバンドルが正常であることを知ることができます。

インストール

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());
});

プリロード ブリッジ

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);
});

新しいバンドルをデプロイ

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

実践的なアドバイス

  • __CAPGO_KEEP_0__ を常に notifyAppReady() __CAPGO_KEEP_0__ をレンダラー内で早く呼び出すようにして、ロールバック保護が正しく機能するようにします。
  • 組み込まれたパスを安定させて、アップデーターが配布されたバンドルかダウンロードしたバンドルを読み込むかを判断するようにします。
  • Capgo アプリを再利用して、既存のモバイルアプリと同じバックエンドリリースパイプラインを共有するElectronアプリの場合、同じCapgo チャネルとロールアウトモデルを使用してください。