Langsung ke konten

Memulai

Terminal window
npm install @capgo/capacitor-updater
npx cap sync

Bagi sebagian besar pengguna, sebaiknya ikuti panduan Memulai Cepat utama yang mencakup instalasi plugin dan integrasi cloud Capgo.

Panduan memulai ini berfokus pada detail plugin teknis untuk pengguna tingkat lanjut yang ingin memahami mekanisme dasar atau menerapkan pembaruan yang dihosting sendiri.

Plugin Capacitor Updater memungkinkan pembaruan over-the-air (OTA) untuk aplikasi Capacitor Anda. Hal ini memungkinkan Anda untuk mendorong pembaruan ke aplikasi Anda tanpa melalui ulasan toko aplikasi.

  1. Unduhan Paket: Plugin mengunduh paket pembaruan (file ZIP yang berisi aset web Anda)
  2. Ekstraksi: Kumpulan diekstraksi ke penyimpanan perangkat
  3. Hot Reload: Aplikasi beralih ke paket baru tanpa perlu memulai ulang
  4. Fallback: Jika pembaruan gagal, aplikasi akan kembali ke versi kerja sebelumnya

Cara paling sederhana untuk menggunakan plugin dengan manajemen pembaruan otomatis:

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

Tambahkan ke capacitor.config.ts Anda:

{
plugins: {
CapacitorUpdater: {
autoUpdate: true,
updateUrl: 'https://your-update-server.com/api/updates'
}
}
}

Untuk kontrol lanjutan atas proses pembaruan:

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

Tidak diperlukan konfigurasi tambahan. Plugin ini berfungsi dengan baik.

Tidak diperlukan konfigurasi tambahan. Plugin ini berfungsi dengan baik.

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

Dengarkan acara pembaruan:

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

Konfigurasikan plugin di capacitor.config.ts Anda:

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

Cara termudah untuk memulai:

// Install the Capgo CLI
npm install -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

Lihat panduan Memulai Cepat utama untuk detailnya.

Host server pembaruan Anda sendiri:

// Configure your update endpoint
{
plugins: {
CapacitorUpdater: {
autoUpdate: true,
updateUrl: 'https://your-server.com/api/check-update'
}
}
}

Server Anda harus kembali:

{
"version": "1.0.1",
"url": "https://your-server.com/updates/1.0.1.zip"
}

Lihat Mode yang Dihosting Sendiri untuk detail selengkapnya.

Kontrol penuh atas pembaruan:

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();
}
  • Selalu hubungi notifyAppReady() ketika aplikasi Anda berhasil dimuat
  • Uji pembaruan secara menyeluruh sebelum mendorong ke produksi
  • Menerapkan penanganan kesalahan yang tepat untuk kegagalan jaringan
  • Gunakan nomor versi secara konsisten
  • Jaga agar ukuran bundel tetap kecil untuk pengunduhan yang lebih cepat
  • Pantau tingkat keberhasilan pembaruan