跳过内容

自动更新

GitHub

本文档将解释如何运行您的自动更新服务器。

确保您的捆绑包通过 HTTPS 服务,并且服务器有正确的 CORS 头来允许应用程序下载更新。 例如: https://myserver.com/app/updates/updates.json

如果您不熟悉服务捆绑包,我们建议您尝试 Capgo 云或查看以下示例:

添加一个 updateUrl 到你的 capacitor.config.json.

{
"plugins": {
"CapacitorUpdater": {
"updateUrl": "https://myserver.com/app/updates/updates.json",
}
}
}

插件将每次打开应用时向你的 API 进行 POST 请求,请求体如下:

interface AppInfos {
"platform": "ios" | "android" | "electron",
"device_id": "UUID_of_device_unique_by_install",
"app_id": "APPID_FROM_CAPACITOR_CONFIG",
"custom_id": "your_custom_id_set_on_runtime",
"plugin_version": "PLUGIN_VERSION",
"version_build": "VERSION_NUMBER_FROM_NATIVE_CODE",
"version_code": "VERSION_CODE_FROM_NATIVE_CODE",
"version_name": "LAST_DOWNLOADER_VERSION" | "builtin"
"version_os": "VERSION_OF_SYSTEM_OS",
"is_emulator": boolean,
"is_prod": boolean,
}

API 服务器应以 JSON 格式响应,capacitor-updater 插件。使用此数据,如果需要更新:

{
"version": "1.2.3",
"url": "https://myserver.com/app/updates/my-new-app-2.0.0.zip",
"checksum": "sha256_checksum_of_bundle"
}

在自动更新模式下,服务器应比较版本并返回正确的版本,如果 URL 键存在,插件将启动下载过程。

如果您添加“message”和“error”键,版本将不会被设置,消息将在日志中显示而不是版本。

version 键应位于 semver 格式。

zip 应该有 index.html 作为根目录的文件,或根目录只有一个文件夹 index.html 内。

您可以使用 CLI 命令将您的捆绑包打包成 zip:

创建一个包含您要从服务器服务的文件的捆绑包
npx @capgo/cli bundle zip --path [/path/to/my/bundle]

生成包文件校验和

生成包文件校验和

重要: You must use the Capgo CLI to create your bundle zip file. The Capgo plugin requires a specific zip format and structure that is only guaranteed when using the official CLI tool. Standard zip utilities may create incompatible archives.

要为您的包生成校验和,请使用 Capgo CLI zip 命令并指定 --json 标志:

创建包含校验信息的包
npx @capgo/cli bundle zip [appId] --json

此命令将:

  • 创建一个与 Capgo 插件兼容的正确格式的 zip 文件
  • 生成 SHA256 校验和以进行完整性验证
  • 输出包信息以 JSON 格式

示例输出:

{
"version": "1.2.3",
"checksum": "a1b2c3d4e5f6789...",
"size": 1234567
}

使用 checksum value from this output in your API response to ensure the plugin can verify the bundle integrity before installation.

继续使用 Auto Update

如果您正在使用

Auto Update 来规划迁移和企业运营,连接它与 使用 @__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-updater 来使用 @capgo/capacitor-updater for the native capability in Using @capgo/capacitor-updater, Capgo 企业版 为 Capgo 企业版 的产品工作流程 Ionic 企业插件替代品 为 Ionic 企业插件替代品 的产品工作流程 Capgo 替代品 在 Capgo 替代品 的产品工作流程中 Capgo 咨询 在 Capgo 咨询 的产品工作流程