跳过内容

开始使用

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.

如果您prefer手动设置,请运行以下命令并遵循以下平台特定的说明:

终端窗口
bun add @capgo/capacitor-updater
bunx cap sync

快速入门

快速入门

对于大多数用户,我们建议遵循 主要快速入门指南 which covers both the plugin installation and Capgo cloud integration.

This getting-started guide focuses on the technical plugin details for advanced users who want to understand the underlying mechanisms or implement self-hosted updates.

The Capacitor Updater plugin enables over-the-air (OTA) updates for your Capacitor applications. This allows you to push updates to your app without going through app store reviews.

Section titled “How It Works”

Bundle Download
  1. : The plugin downloads update bundles (ZIP files containing your web assets)Extraction
  2. : Bundles are extracted to the device’s storage热重载
  3. This getting-started guide focuses on the technical plugin details for advanced users who want to understand the underlying mechanisms or implement self-hosted updates.:应用程序切换到新的捆绑包而无需重启
  4. Fallback:如果更新失败,应用程序会切换到上一个工作版本

Usage Modes

使用模式
使用模式1. 自动更新模式(推荐)

自动更新管理的最简单方法:

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

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

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

基本 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);

事件监听器

事件监听器

监听更新事件:

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'
}
}
}

集成模式

集成模式

使用 Capgo 云

使用 Capgo 云

开始的最简单方法:

// 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 为native插件工作做好准备,连接它 使用@capgo/capacitor-updater 在使用@capgo/capacitor-updater时,native能力 Capgo插件目录 在Capgo插件目录中,产品工作流 Capacitor由Capgo提供的插件 在Capacitor由Capgo提供的插件中,实现细节 添加或更新插件 在添加或更新插件中,实现细节, Ionic企业插件替代品 在Ionic企业插件替代品中,产品工作流