Getting Started
复制一个包含安装步骤和此插件的完整Markdown指南的设置提示。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-updater`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/updater/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
标题为“安装”您可以使用我们的 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-updaterbunx cap syncyarn add @capgo/capacitor-updaterbunx cap syncpnpm add @capgo/capacitor-updaterbunx cap syncbun add @capgo/capacitor-updaterbunx cap sync快速入门
快速入门部分对于大多数用户,我们建议遵循 快速入门指南 该指南涵盖了插件安装和Capgo云集成。
本入门指南侧重于技术插件细节,适合高级用户了解底层机制或自行实现更新。
概述
概述部分Capacitor更新器插件支持对Capacitor应用进行远程更新。这使您可以将更新推送到您的应用程序,而无需通过应用商店进行审查。
如何工作
如何工作- 下载更新包:插件下载更新包(ZIP文件,包含您的Web资产)
- 提取:提取包到设备的存储中
- 热重载: 应用程序切换到新捆绑包而无需重新启动
- 回退: 如果更新失败,应用程序会切换回上一个工作版本
使用模式
使用模式1. 自动更新模式(推荐)
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' } }}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
无需额外配置。插件即可使用。
Copy to clipboard平台配置
基本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 startawait CapacitorUpdater.set({ id: bundle.id});复制到剪贴板
使用新包重新加载// Reload app immediately with new bundleawait 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 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: '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 CLIbun add -g @capgo/cli
// Login to Capgonpx @capgo/cli login
// Upload your first bundlenpx @capgo/cli bundle upload
// The plugin auto-updates from Capgo cloud查看 主快速入门指南 for details.
自主托管更新
自主托管更新托管自己的更新服务器:
// 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"}查看 自主托管模式 for complete details.
手动更新流程
自主托管更新流程完全控制更新:
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()在推送到生产环境之前测试更新 - 为网络故障实现适当的错误处理
- 一致使用版本号
- 保持捆绑包大小小以便快速下载
- 监控更新成功率
- 下一步行动
__CAPGO_KEEP_0__
下一步支持
标题:支持从 Getting Started 继续前进
标题:从 Getting Started 继续前进如果您正在使用 开始使用 为了native插件的工作,连接它与 使用@capgo/capacitor-updater 对于native能力在使用@capgo/capacitor-updater, Capgo插件目录 对于产品工作流程在Capgo插件目录, Capacitor插件由Capgo 对于实现细节在Capacitor插件由Capgo, 添加或更新插件 对于实现细节在添加或更新插件,和 Ionic企业插件替代方案 对于产品工作流程在Ionic企业插件替代方案.