入门
npm install @capgo/capacitor-updaternpx cap syncyarn add @capgo/capacitor-updaternpx cap syncpnpm add @capgo/capacitor-updaternpx cap syncbun add @capgo/capacitor-updaternpx cap sync对于大多数用户,我们建议遵循主要快速入门指南,其中涵盖插件安装和 Capgo 云集成。
本入门指南重点关注想要了解底层机制或实施自托管更新的高级用户的技术插件详细信息。
Capacitor 更新程序插件可为您的 Capacitor 应用程序启用无线 (OTA) 更新。这使您可以将更新推送到您的应用程序,而无需经过应用程序商店评论。
它是如何工作的
Section titled “它是如何工作的”- 捆绑包下载:插件下载更新捆绑包(包含您的网络资产的 ZIP 文件)
- 提取:捆绑包被提取到设备的存储中
- 热重载:应用程序无需重新启动即可切换到新捆绑包
- 回退:如果更新失败,应用程序将恢复到之前的工作版本
1.自动更新模式(推荐)
Section titled “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' } }}2. 手动模式
Section titled “2. 手动模式”对于更新过程的高级控制:
import { CapacitorUpdater } from '@capgo/capacitor-updater';
// Download an updateconst 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 immediatelyawait CapacitorUpdater.reload();无需额外配置。该插件开箱即用。
Android
Section titled “Android”无需额外配置。该插件开箱即用。
基本 API 用法
Section titled “基本 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);设置活动捆绑包
Section titled “设置活动捆绑包”// Set bundle to be used on next app startawait CapacitorUpdater.set({ id: bundle.id});重新加载新包
Section titled “重新加载新包”// Reload app immediately with new bundleawait CapacitorUpdater.reload();const { bundles } = await CapacitorUpdater.list();console.log('Available bundles:', bundles);await CapacitorUpdater.delete({ id: 'bundle-id'});获取当前捆绑包
Section titled “获取当前捆绑包”const { bundle } = await CapacitorUpdater.current();console.log('Current bundle:', bundle.version);监听更新事件:
import { CapacitorUpdater } from '@capgo/capacitor-updater';
// Listen for download progressCapacitorUpdater.addListener('download', (info) => { console.log('Download progress:', info.percent);});
// Listen for download completionCapacitorUpdater.addListener('downloadComplete', (bundle) => { console.log('Download complete:', bundle.version);});
// Listen for update failuresCapacitorUpdater.addListener('updateFailed', (error) => { console.error('Update failed:', error);});
// Listen for successful updatesCapacitorUpdater.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' } }}使用 Capgo 云
Section titled “使用 Capgo 云”最简单的入门方法:
// Install the Capgo CLInpm install -g @capgo/cli
// Login to Capgonpx @capgo/cli login
// Upload your first bundlenpx @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"}有关完整详细信息,请参阅自托管模式。
手动更新流程
Section titled “手动更新流程”完全控制更新:
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() - 在投入生产之前彻底测试更新
- 对网络故障实施适当的错误处理
- 一致地使用版本号
- 保持较小的捆绑包大小以加快下载速度
- 监控更新成功率