跳过内容

开始

GitHub

您可以使用我们的 AI 助手设置来安装插件。将 Capgo 技能添加到您的 AI 工具中,使用以下命令:

终端窗口
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

然后使用以下提示:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-updater` plugin in my project.

如果您更喜欢手动设置,请按照以下命令安装插件并遵循以下平台特定的说明:

笔合突算
bun add @capgo/capacitor-updater
bunx cap sync

快速入门

快速入门

对于大多数用户,我们建议遵循 主快速入门指南 该指南涵盖了插件安装和Capgo云集成。

本指南侧重于技术插件细节,适合高级用户了解底层机制或实现自主更新。

概述

概述

Capacitor更新插件支持Capacitor应用程序的即时更新(OTA)。这使您可以将更新推送到您的应用程序,而无需通过应用商店进行审查。

如何工作

如何工作
  1. __CAPGO_KEEP_0__更新插件允许您将更新推送到您的__CAPGO_KEEP_1__应用程序,而无需通过应用商店进行审查。: 这个插件下载更新包(包含您的网页资产的 ZIP 文件)
  2. Extraction: 包被提取到设备的存储中
  3. Hot Reload: 应用程序切换到新包而无需重启
  4. Fallback: 如果更新失败,应用程序会切回上一个工作版本

使用插件进行自动更新管理的最简单方法:

import { CapacitorUpdater } from '@capgo/capacitor-updater';
// Plugin handles everything automatically
// Configure in capacitor.config.ts

添加到您的 capacitor.config.ts:

{
plugins: {
CapacitorUpdater: {
autoUpdate: 'atBackground',
updateUrl: 'https://your-update-server.com/api/updates'
}
}
}

对于更新过程的高级控制:

import { CapacitorUpdater } from '@capgo/capacitor-updater';
// Download an update
const bundle = await CapacitorUpdater.download({
url: 'https://your-server.com/updates/v1.0.1.zip',
version: '1.0.1'
});
// Set the bundle (will be used on next app start)
await CapacitorUpdater.set({
id: bundle.id
});
// Or reload immediately
await CapacitorUpdater.reload();

不需要额外的配置。插件可以直接使用。

Android

Android

无需额外配置。插件可以直接使用。

基本API使用

基本API使用

下载更新

下载更新
import { CapacitorUpdater } from '@capgo/capacitor-updater';
const bundle = await CapacitorUpdater.download({
url: 'https://example.com/update.zip',
version: '1.0.1'
});
console.log('Downloaded bundle:', bundle.id);

设置活动包

复制到剪贴板
// Set bundle to be used on next app start
await CapacitorUpdater.set({
id: bundle.id
});
// Reload app immediately with new bundle
await CapacitorUpdater.reload();
const { bundles } = await CapacitorUpdater.list();
console.log('Available bundles:', bundles);
await CapacitorUpdater.delete({
id: 'bundle-id'
});
const { bundle } = await CapacitorUpdater.current();
console.log('Current bundle:', bundle.version);

Listen for update events:

import { CapacitorUpdater } from '@capgo/capacitor-updater';
// Listen for download progress
CapacitorUpdater.addListener('download', (info) => {
console.log('Download progress:', info.percent);
});
// Listen for download completion
CapacitorUpdater.addListener('downloadComplete', (bundle) => {
console.log('Download complete:', bundle.version);
});
// Listen for update failures
CapacitorUpdater.addListener('updateFailed', (error) => {
console.error('Update failed:', error);
});
// Listen for successful updates
CapacitorUpdater.addListener('updateAvailable', (info) => {
console.log('Update available:', info.version);
});

在您的 capacitor.config.ts:

{
plugins: {
CapacitorUpdater: {
// Auto-update settings
autoUpdate: 'atBackground',
updateUrl: 'https://api.example.com/updates',
// Update behavior
resetWhenUpdate: true,
// Version settings
version: '1.0.0',
// Security
allowModifyUrl: false,
// Stats collection
statsUrl: 'https://api.example.com/stats',
// Channel (for Capgo cloud)
defaultChannel: 'production'
}
}
}

开始的最简单方法:

// Install the Capgo CLI
bun add -g @capgo/cli
// Login to Capgo
npx @capgo/cli login
// Upload your first bundle
npx @capgo/cli bundle upload
// The plugin auto-updates from Capgo cloud

查看 主页快速入门指南 详细信息。

托管自己的更新服务器:

// Configure your update endpoint
{
plugins: {
CapacitorUpdater: {
autoUpdate: 'atBackground',
updateUrl: 'https://your-server.com/api/check-update'
}
}
}

您的服务器应返回:

{
"version": "1.0.1",
"url": "https://your-server.com/updates/1.0.1.zip"
}

查看 自主托管模式 了解更多详细信息。

手动更新流程

手动更新流程

对更新有完全的控制权:

import { CapacitorUpdater } from '@capgo/capacitor-updater';
async function checkAndUpdate() {
// Check for updates from your server
const response = await fetch('https://api.example.com/check-update');
const { version, url } = await response.json();
// Download the update
const bundle = await CapacitorUpdater.download({
url,
version
});
// Notify bundle is ready
await CapacitorUpdater.notifyAppReady();
// Set as next version
await CapacitorUpdater.set({ id: bundle.id });
// Reload when ready
await CapacitorUpdater.reload();
}

最佳实践

手动更新流程
  • 当您的应用程序成功加载时 notifyAppReady() 在推送到生产环境之前测试更新
  • 实现网络故障时的适当错误处理
  • 一致使用版本号
  • 手动更新流程
  • 保持捆绑包大小以便于下载
  • 监控更新成功率

下一步

下一步

支持

支持

继续从 Getting Started

标题:继续从 Getting Started

如果您正在使用 Getting Started 来规划原生插件工作,连接它与 使用 @capgo/capacitor-updater 为原生能力在使用 @capgo/capacitor-updater 中 Capgo 插件目录 为产品工作流程在 Capgo 插件目录 中 Capacitor 插件由 Capgo 为实现细节在 Capacitor 插件由 Capgo 中 添加或更新插件 了解如何在添加或更新插件的过程中实现详细信息,和 Ionic 企业插件替代品 了解Ionic 企业插件替代品的产品工作流程。