@capgo/capacitor-webview-guardian

June 16, 2026 · View on GitHub

Capgo - Instant updates for Capacitor

➡️ Get Instant updates for your App with Capgo

Missing a feature? We’ll build the plugin for you 💪

Keep your Capacitor app alive after the OS kills its WebView while the app is in the background. Webview Guardian listens for foreground events, probes the renderer, and reloads it automatically (or notifies you so you can recover your own state) when the render process was terminated.

Compatibility

Plugin versionCapacitor compatibilityMaintained
v8.*.*v8.*.*
v7.*.*v7.*.*On demand
v6.*.*v6.*.*
v5.*.*v5.*.*

Note: The major version of this plugin follows the major version of Capacitor. Use the version that matches your Capacitor installation (e.g., plugin v8 for Capacitor 8). Only the latest major version is actively maintained.

Install

You can use our AI-Assisted Setup to install the plugin. Add the Capgo skills to your AI tool using the following command:

npx skills add https://github.com/cap-go/capacitor-skills --skill capacitor-plugins

Then use the following prompt:

Use the `capacitor-plugins` skill from `cap-go/capacitor-skills` to install the `@capgo/capacitor-webview-guardian` plugin in my project.

If you prefer Manual Setup, install the plugin by running the following commands and follow the platform-specific instructions below:

npm install @capgo/capacitor-webview-guardian
npx cap sync

Usage

import { WebviewGuardian } from '@capgo/capacitor-webview-guardian';

await WebviewGuardian.startMonitoring({
  foregroundDebounceMs: 800,
  autoRestart: true,
  restartStrategy: 'reload',
});

WebviewGuardian.addListener('webviewRestarted', ({ reason }) => {
  console.info('[guardian] WebView restarted', reason);
});

API

startMonitoring(...)

startMonitoring(options?: StartMonitoringOptions | undefined) => Promise<GuardianState>

Starts observing foreground events and automatically checks the WebView health.

ParamType
optionsStartMonitoringOptions

Returns: Promise<GuardianState>


stopMonitoring()

stopMonitoring() => Promise<GuardianState>

Stops any automatic foreground monitoring.

Returns: Promise<GuardianState>


getState()

getState() => Promise<GuardianState>

Returns the latest known monitoring state.

Returns: Promise<GuardianState>


checkNow(...)

checkNow(options?: CheckNowOptions | undefined) => Promise<CheckResult>

Forces a WebView health probe immediately.

ParamType
optionsCheckNowOptions

Returns: Promise<CheckResult>


addListener('foreground' | 'webviewHealthy' | 'webviewCrashed' | 'webviewRestarted', ...)

addListener(eventName: 'foreground' | 'webviewHealthy' | 'webviewCrashed' | 'webviewRestarted', listenerFunc: (event: GuardianEvent) => void) => Promise<PluginListenerHandle>
ParamType
eventName'foreground' | 'webviewHealthy' | 'webviewCrashed' | 'webviewRestarted'
listenerFunc(event: GuardianState) => void

Returns: Promise<PluginListenerHandle>


Interfaces

GuardianState

PropType
monitoringboolean
reasonstring
timestampstring
lastHealthyAtstring
lastRestartAtstring
lastCrashAtstring
pendingRestartReasonstring
errorstring

StartMonitoringOptions

PropTypeDescription
foregroundDebounceMsnumberDelay (in ms) before running a health check after the app re-enters the foreground. Defaults to 600ms to let the bridge finish resuming.
pingScriptstringScript executed via evaluateJavascript/evaluateJavaScript to confirm the WebView is alive. Defaults to document.readyState.
autoRestartbooleanAutomatically reloads the WebView when a terminated render process is detected. Disable to receive webviewCrashed events and restart manually.
restartStrategyRestartStrategyStrategy used when restarting the WebView. Defaults to reload.
customRestartUrlstringCustom HTTPS/HTTP URL to load when restartStrategy is customUrl.
debugbooleanEmits verbose logging in the native layer when true.
runInitialCheckbooleanWhether an immediate health check should be executed right after enabling monitoring. Defaults to true.

CheckResult

PropType
healthyboolean
restartedboolean
reasonstring
timestampstring
errorstring
pendingRestartboolean

CheckNowOptions

PropTypeDescription
reasonstringText tag describing why a manual check is being requested.

PluginListenerHandle

PropType
remove() => Promise<void>

Type Aliases

RestartStrategy

'reload' | 'reloadFromOrigin' | 'customUrl'

GuardianEvent

GuardianState