跳转到正文

Cordova Updater 入门指南

GitHub

本指南将指导您添加 @capgo/cordova-updater 将其添加到 Cordova 应用,以便在 iOS 和 Android 上接收 Capgo 实时更新。

  • 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. 修改插件变量后,准备原生项目:

    终端窗口
    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 资源。

上传你的第一个 bundle

“上传你的第一个 bundle”部分

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

终端窗口
bun run build
npx @capgo/cli@latest bundle upload --channel=production
  • 与以下设置进行对比 Capacitor updater 配置 — 大多数选项对应 plugin.xml 变量。
  • 读取 Live update 兼容性 在发布原生插件变更之前。
  • 以后要迁移到 Capacitor?保持相同的 Capgo 应用 ID 和通道不变;只需更换客户端插件包。