JSPM

@capgo/capacitor-updater

4.0.0-alpha.15
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 35210
  • Score
    100M100P100Q160999F
  • License AGPL-3.0-only

OTA update for capacitor apps

Package Exports

  • @capgo/capacitor-updater
  • @capgo/capacitor-updater/dist/esm/index.js
  • @capgo/capacitor-updater/dist/plugin.cjs.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@capgo/capacitor-updater) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

capacitor-updater

Update Ionic Capacitor apps without App/Play Store review (Code-push / hot-code updates).

Usage options:

  • use capgo.app, a full featured auto update system. (5 min Setup, easily manage versions, update, revert, and see detailed stats.)
  • integrate your own API with this plugin's auto update system.
  • use manual methods to zip, upload, download, from application code - you have full control.

Community

Join the discord to get help.

Documentation

I maintain a more user friendly and complete documentation in GitHub wiki.

Installation

npm install @capgo/capacitor-updater
npx cap sync

Auto-update setup

Create account in capgo.app and get your API key

  • Download the CLI npm i -g @capgo/cli
  • Add app from CLI capgo add -a API_KEY
  • Upload app capgo upload -a API_KEY
  • Upload app capgo set -a API_KEY -s public
  • Edit your capacitor.config.json like below, set autoUpdate to true.
// capacitor.config.json
{
    "appId": "**.***.**",
    "appName": "Name",
    "plugins": {
        "CapacitorUpdater": {
            "autoUpdate": true,
        }
    }
}
  • Add to your main code
  import { CapacitorUpdater } from '@capgo/capacitor-updater'
  CapacitorUpdater.notifyAppReady()

This tells Capacitor Updator that the current update bundle has loaded succesfully. Failing to call this method will cause your application to be rolled back to the previously successful version (or built-in bundle).

  • Do npm run build && npx cap copy to copy the build to capacitor.
  • Run the app and see app auto update after each backgrounding.
  • Failed updates will automatically roll back to the last successful version.

See more details in the Auto update documentation.

Manual setup

Download update distribution zipfiles from a custom url. Manually control the entire update process.

  • Add to your main code
  import { CapacitorUpdater } from '@capgo/capacitor-updater'
  CapacitorUpdater.notifyAppReady()

This informs Capacitor Updator that the current update bundle has loaded succesfully. Failing to call this method will cause your application to be rolled back to the previously successful version (or built-in bundle).

  • Add this to your application.
  const version = await CapacitorUpdater.download({
    url: 'https://github.com/Cap-go/demo-app/releases/download/0.0.4/dist.zip',
  })
  await CapacitorUpdater.set(version); // sets the new version, and reloads the app
  • Failed updates will automatically roll back to the last successful version.
  • Example: Using App-state to control updates, with SplashScreen: You might also consider performing auto-update when application state changes, and using the Splash Screen to improve user experience.
  import { CapacitorUpdater, VersionInfo } from '@capgo/capacitor-updater'
  import { SplashScreen } from '@capacitor/splash-screen'
  import { App } from '@capacitor/app'

  let version: VersionInfo;
  App.addListener('appStateChange', async (state) => {
      if (state.isActive) {
        // Ensure download occurs while the app is active, or download may fail
        version = await CapacitorUpdater.download({
          url: 'https://github.com/Cap-go/demo-app/releases/download/0.0.4/dist.zip',
        })
      }

      if (!state.isActive && version) {
        // Activate the update when the application is sent to background
        SplashScreen.show()
        try {
          await CapacitorUpdater.set(version);
          // At this point, the new version should be active, and will need to hide the splash screen
        } catch () {
          SplashScreen.hide() // Hide the splash screen again if something went wrong
        }
      }
  })

TIP: If you prefer a secure and automated way to update your app, you can use capgo.app - a full-featured, auto update system.

Packaging dist.zip update bundles

Capacitor Updator works by unzipping a compiled app bundle to the native device filesystem. Whatever you choose to name the file you upload/download from your release/update server URL (via either manual or automatic updating), this .zip bundle must meet the following requirements:

  • The zip file should contain the full contents of your production Capacitor build output folder, usually {project directory}/dist/ or {project directory}/www/. This is where index.html will be located, and it should also contain all bundled JavaScript, CSS, and web resources necessary for your app to run.
  • Do not password encrypt the bundle zip file, or it will fail to unpack.
  • Make sure the bundle does not contain any extra hidden files or folders, or it may fail to unpack.

API

notifyAppReady()

notifyAppReady() => Promise<BundleInfo>

Notify Capacitor Updater that the current bundle is working (a rollback will occur of this method is not called on every app launch)

Returns: Promise<BundleInfo>


download(...)

download(options: { url: string; version?: string; }) => Promise<BundleInfo>

Download a new version from the provided URL, it should be a zip file, with files inside or with a unique id inside with all your files

Param Type
options { url: string; version?: string; }

Returns: Promise<BundleInfo>


next(...)

next(options: { id: string; }) => Promise<BundleInfo>

Set the next bundle to be used when the app is reloaded.

Param Type
options { id: string; }

Returns: Promise<BundleInfo>


set(...)

set(options: { id: string; }) => Promise<void>

Set the current bundle and immediately reloads the app.

Param Type
options { id: string; }

delete(...)

delete(options: { id: string; }) => Promise<void>

Delete bundle in storage

Param Type
options { id: string; }

list()

list() => Promise<{ bundles: BundleInfo[]; }>

Get all available versions

Returns: Promise<{ bundles: BundleInfo[]; }>


reset(...)

reset(options?: { toLastSuccessful?: boolean | undefined; } | undefined) => Promise<void>

Set the builtin version (the one sent to Apple store / Google play store ) as current version

Param Type
options { toLastSuccessful?: boolean; }

current()

current() => Promise<{ bundle: BundleInfo; native: string; }>

Get the current bundle, if none are set it returns builtin, currentNative is the original bundle installed on the device

Returns: Promise<{ bundle: BundleInfo; native: string; }>


reload()

reload() => Promise<void>

Reload the view


setDelay(...)

setDelay(options: { kind: DelayUntilNext; value?: string; }) => Promise<void>

Set delay to skip updates in the next time the app goes into the background

Param Type
options { kind: DelayUntilNext; value?: string; }

Since: 4.0.0


cancelDelay()

cancelDelay() => Promise<void>

Cancel delay to updates as usual

Since: 4.0.0


getLatest(...)

getLatest(options: { delay: boolean; }) => Promise<latestVersion>

Get Latest version available from update Url

Param Type
options { delay: boolean; }

Returns: Promise<latestVersion>

Since: 4.0.0


addListener('download', ...)

addListener(eventName: 'download', listenerFunc: DownloadChangeListener) => Promise<PluginListenerHandle> & PluginListenerHandle

Listen for download event in the App, let you know when the download is started, loading and finished

Param Type
eventName 'download'
listenerFunc DownloadChangeListener

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

Since: 2.0.11


addListener('downloadComplete', ...)

addListener(eventName: 'downloadComplete', listenerFunc: DownloadCompleteListener) => Promise<PluginListenerHandle> & PluginListenerHandle

Listen for download event in the App, let you know when the download is started, loading and finished

Param Type
eventName 'downloadComplete'
listenerFunc DownloadCompleteListener

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

Since: 4.0.0


addListener('majorAvailable', ...)

addListener(eventName: 'majorAvailable', listenerFunc: MajorAvailableListener) => Promise<PluginListenerHandle> & PluginListenerHandle

Listen for Major update event in the App, let you know when major update is blocked by setting disableAutoUpdateBreaking

Param Type
eventName 'majorAvailable'
listenerFunc MajorAvailableListener

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

Since: 2.3.0


addListener('updateFailed', ...)

addListener(eventName: 'updateFailed', listenerFunc: UpdateFailedListener) => Promise<PluginListenerHandle> & PluginListenerHandle

Listen for update event in the App, let you know when update is ready to install at next app start

Param Type
eventName 'updateFailed'
listenerFunc UpdateFailedListener

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

Since: 2.3.0


getId()

getId() => Promise<{ id: string; }>

Get unique ID used to identify device (sent to auto update server)

Returns: Promise<{ id: string; }>


getPluginVersion()

getPluginVersion() => Promise<{ version: string; }>

Get the native Capacitor Updater plugin version (sent to auto update server)

Returns: Promise<{ version: string; }>


isAutoUpdateEnabled()

isAutoUpdateEnabled() => Promise<{ enabled: boolean; }>

Get the state of auto update config. This will return false in manual mode.

Returns: Promise<{ enabled: boolean; }>


addListener(string, ...)

addListener(eventName: string, listenerFunc: (...args: any[]) => any) => Promise<PluginListenerHandle>
Param Type
eventName string
listenerFunc (...args: any[]) => any

Returns: Promise<PluginListenerHandle>


removeAllListeners()

removeAllListeners() => Promise<void>

Interfaces

BundleInfo

Prop Type
id string
version string
downloaded string
status BundleStatus

latestVersion

Prop Type Description Since
version string Res of getLatest method 4.0.0
major boolean
message string
old string
url string

PluginListenerHandle

Prop Type
remove () => Promise<void>

DownloadEvent

Prop Type Description Since
percent number Current status of download, between 0 and 100. 4.0.0
bundle BundleInfo

DownloadCompleteEvent

Prop Type Description Since
bundle BundleInfo Emit when a new update is available. 4.0.0

MajorAvailableEvent

Prop Type Description Since
version string Emit when a new major version is available. 4.0.0

UpdateFailedEvent

Prop Type Description Since
bundle BundleInfo Emit when a update failed to install. 4.0.0

Type Aliases

BundleStatus

'success' | 'error' | 'pending' | 'downloading'

DelayUntilNext

'background' | 'kill' | 'nativeVersion' | 'date'

DownloadChangeListener

(state: DownloadEvent): void

DownloadCompleteListener

(state: DownloadCompleteEvent): void

MajorAvailableListener

(state: MajorAvailableEvent): void

UpdateFailedListener

(state: UpdateFailedEvent): void

Listen to download events

  import { CapacitorUpdater } from '@capgo/capacitor-updater';

CapacitorUpdater.addListener('download', (info: any) => {
  console.log('download was fired', info.percent);
});

On iOS, Apple don't allow you to show a message when the app is updated, so you can't show a progress bar.

Inspiration

Contributors

jamesyoung1337 Thanks a lot for your guidance and support, it was impossible to make this plugin work without you.