跳过内容

Getting Started

GitHub

安装

安装
复制到剪贴板
bun add @capgo/capacitor-updater
bunx cap sync

快速入门

快速入门部分

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

本入门指南重点介绍了技术插件细节,适合高级用户了解底层机制或自行实现更新。

概述

概述

Capacitor 升级插件支持Capacitor应用的即时更新。您可以通过此功能将更新推送到您的应用程序,无需通过应用商店进行审查。

如何工作

如何工作
  1. 下载包: 插件下载更新包(包含您的Web资产的ZIP文件)
  2. 提取: 包被提取到设备的存储中
  3. 热重载: 应用程序切换到新包,无需重启
  4. 回退: 如果更新失败,应用程序会恢复到上一个工作版本

使用模式

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

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

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

2. 手动模式

手动模式

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

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

平台配置

平台配置

iOS

iOS

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

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

监听更新事件:

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 来规划原生插件工作,连接它与 使用 @capgo/capacitor-updater 为使用 @capgo/capacitor-updater 的本地能力 Capgo 插件目录 为 Capgo 插件目录中的产品流程 Capacitor 由 Capgo 提供的插件 为 Capacitor 由 Capgo 提供的插件中的实现细节 添加或更新插件 为添加或更新插件中的实现细节 Ionic 企业插件替代方案 为 Ionic 企业插件替代方案中的产品流程