跳过内容

开始使用

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

快速入门

快速入门

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

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

概述

概述

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.

如何工作

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

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

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

添加到你的 capacitor.config.ts:

{
plugins: {
CapacitorUpdater: {
autoUpdate: true,
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: true,
updateUrl: 'https://api.example.com/updates',
// Update behavior
resetWhenUpdate: true,
directUpdate: false,
// 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: true,
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() 在推送到生产环境之前测试更新
  • 实现网络故障处理的适当错误处理
  • 在应用程序成功加载时
  • 保持版本号一致
  • 尽量减小打包大小,减少下载时间
  • 监控更新成功率

支持

支持