跳转至正文

Cordova Updater 入门指南

GitHub

本指南将带你完成添加 @capgo/cordova-updater 到 Cordova 应用,这样就能在 iOS 和 Android 上接收 Capgo live updates。

  • Cordova CLI 12+
  • Cordova Android 13+ 和/或 Cordova iOS 7+
  • 拥有 Capgo 账号 (capgo.app/register)
  1. 从 npm 安装插件,并传入您的 Capgo App ID:

    终端窗口
    cordova plugin add @capgo/cordova-updater --variable APP_ID=YOUR_CAPGO_APP_ID
  2. 可选:在安装时覆盖默认配置 (变量与 Capacitor updater 相同):

    终端窗口
    cordova plugin add @capgo/cordova-updater \
    --variable APP_ID=YOUR_CAPGO_APP_ID \
    --variable DEFAULT_CHANNEL=production \
    --variable UPDATE_URL=https://plugin.capgo.app/updates \
    --variable AUTO_UPDATE=atBackground
  3. 修改插件变量后,请准备 Native Build 项目:

    终端窗口
    cordova prepare android ios
document.addEventListener('deviceready', async () => {
await cordova.plugins.Updater.notifyAppReady();
});
  • 请勿将此插件与 cordova-plugin-ionic-webview 配合使用。
  • 调用 notifyAppReady() bundle 一加载完就执行——赶在繁重 UI 工作拖慢回滚窗口之前。
  • 请确保 Cordova scheme 设置正确。

在 deviceready,该插件可通过以下方式使用: cordova.plugins.Updater:

document.addEventListener('deviceready', async () => {
const { Updater } = cordova.plugins;
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 });
}
});

npm 包已内置 TypeScript 类型:

import type { UpdaterPlugin } from '@capgo/cordova-updater';
declare const cordova: { plugins: { Updater: UpdaterPlugin } };
  • 需要 Cordova Android 13+,以便 WebView 通过默认 https://localhost/ scheme 提供服务。
  • 该插件会注册一个 CordovaPluginPathHandler 用于提供下载的 bundle 资源。

构建你的 web 资源,然后使用 Capgo CLI 进行上传(工作流与 Capacitor 相同):

终端窗口
bun run build
npx @capgo/cli@latest bundle upload --channel=production