@capgo/capacitor-webview-crash

中文文档
8.1.13发布于 2026/10/15MPL-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 7.3万

Capacitor plugin for detecting WebView crash recovery and restarting long-running WebViews natively.

原始使用文档
文档目录20 节
  1. ➡️ Get started for free
  2. Key features
  3. What It Does
  4. What It Does Not Do
  5. Compatibility
  6. Install
  7. Usage
  8. Native Auto Restart
  9. Config Type
  10. Platform Notes
  11. Documentation
  12. API
  13. getPendingCrashInfo()
  14. clearPendingCrashInfo()
  15. simulateCrashRecovery()
  16. restartWebView()
  17. addListener('webViewRestoredAfterCrash' | 'webViewRestoredA…
  18. removeAllListeners()
  19. Interfaces
  20. Type Aliases

@capgo/capacitor-webview-crash

Keep your Capacitor app alive when the WebView crashes: detect recovered crashes, restart dead WebViews natively, and recycle long-running WebViews before they run out of memory.

Capgo - Build Capacitor apps with AI, ship updates instantly

Capgo: push fixes to your Capacitor users in minutes, build signed iOS and Android apps without a Mac, and roll back in one click.

➡️ Get started for free

14-day unlimited free trial. No credit card required

🤖 Building with an AI coding agent? Install Capgo skills so Claude Code, Codex, Cursor, Copilot, or Gemini CLI know how to install and use @capgo/capacitor-webview-crash in your Capacitor app: npx skills add Cap-go/capgo-skills. Then ask your agent: "Add @capgo/capacitor-webview-crash to my Capacitor app and wire it up." Agent setup guides · Hosted MCP

@capgo/capacitor-webview-crash for Capacitor apps

Key features

  • Crash recovery: restartOnCrash restarts the WebView when its renderer process dies.
  • Recovery info: getPendingCrashInfo() and clearPendingCrashInfo(), plus the webViewRestoredAfterCrash event.
  • Scheduled recycling: restartIntervalMs or restartCron replaces long-running WebViews.
  • Manual restart: restartWebView() creates a fresh WebView.
  • Testing: simulateCrashRecovery() exercises your recovery flow.
  • Platforms: iOS and Android. Web only supports the testing helpers.

What It Does

  • Stores a native crash marker when Android reports onRenderProcessGone.
  • Hooks the iOS WebView termination callback and persists equivalent crash metadata.
  • Restarts the WebView natively after crashes so recovery does not depend on a still-running JavaScript runtime.
  • Can restart the WebView on a fixed native interval for kiosk, POS, signage, telemetry, and other always-on apps.
  • Lets JavaScript request a native WebView restart with restartWebView() when the app wants to proactively recycle memory.
  • Exposes the marker through an event, a polling method, and a simulation helper for testing recovery flows.
  • Ships a web implementation that simulates the same recovery flow with local storage.

What It Does Not Do

  • Prevent the underlying WebView crash from happening.
  • Restore lost in-memory JavaScript state automatically.
  • Replace application-level state persistence. Persist critical state before enabling scheduled restarts.

Compatibility

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

Policy:

  • New plugins start at version 8.0.0 (Capacitor 8 baseline).
  • Backward compatibility for older Capacitor majors is supported on demand.

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-crash` 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-crash
npx cap sync

Usage

import { WebViewCrash } from '@capgo/capacitor-webview-crash';

await WebViewCrash.addListener('webViewRestoredAfterCrash', async (info) => {
  console.log('Recovered after a WebView crash', info);
  await WebViewCrash.clearPendingCrashInfo();
});

await WebViewCrash.addListener('webViewRestoredAfterRestart', async (info) => {
  console.log('Recovered after a native WebView restart', info);
  await WebViewCrash.clearPendingCrashInfo();
});

const pending = await WebViewCrash.getPendingCrashInfo();
if (pending.value) {
  console.log('Pending crash or restart marker', pending.value);
}

Use simulateCrashRecovery() in development or automated tests to exercise your recovery UI without forcing a real native WebView crash.

Call restartWebView() when the current JavaScript runtime decides the native WebView should be replaced:

await WebViewCrash.restartWebView();

The call writes a pending marker with reason: 'manualRestart', then native code restarts the WebView. Android recreates the host Activity. iOS rebuilds the Capacitor bridge view so a new WKWebView is created instead of reloading the current page.

Native Auto Restart

Configure the plugin in capacitor.config.ts so restart decisions happen in native code, even when the JavaScript runtime is unavailable:

import type { CapacitorConfig } from '@capacitor/cli';
import type { WebViewCrashPluginConfig } from '@capgo/capacitor-webview-crash';

const webViewCrash: WebViewCrashPluginConfig = {
  // Keep native crash recovery enabled. This is the default.
  restartOnCrash: true,

  // Use a 5-field cron schedule in the device local timezone.
  // Do not combine restartCron with an active restartIntervalMs.
  restartCron: '0 3 * * *',

  // Optional delay before restarting after a crash.
  restartAfterCrashDelayMs: 0,
};

const config: CapacitorConfig = {
  plugins: {
    WebViewCrash: webViewCrash,
  },
};

export default config;

Use scheduled restarts for apps that stay open for days: kiosk screens, control-room dashboards, point-of-sale terminals, warehouse scanners, vehicle tablets, or any Capacitor app that cannot rely on users force-closing it. The restart is native, writes a pending marker with reason: 'periodicRestart', and then creates a fresh WebView.

Set restartIntervalMs to a maintenance window that your product can tolerate, or set restartCron for a wall-clock schedule such as 0 3 * * * for a daily 03:00 restart. restartCron uses 5-field cron syntax in the device local timezone and supports *, lists, ranges, and steps. Do not configure both schedules at once: native initialization throws a fatal config error when restartCron is set and restartIntervalMs is greater than 0. The user will get a fresh JavaScript runtime, so persist unsaved form state, queued events, and in-progress work before enabling a short interval or cron schedule.

Config Type

export interface WebViewCrashPluginConfig {
  restartOnCrash?: boolean;
  restartIntervalMs?: number;
  restartCron?: string;
  restartAfterCrashDelayMs?: number;
}

Platform Notes

  • iOS: Uses method swizzling on Capacitor's WebViewDelegationHandler to persist crash metadata before Capacitor reloads the WebView. Manual and scheduled restarts rebuild the Capacitor bridge view so a new WKWebView instance is created. No extra permissions are required.
  • Android: Registers a Capacitor WebViewListener and persists crash metadata from onRenderProcessGone. Crash and scheduled restarts reset the bridge and recreate the host activity, giving the app a fresh WebView. No extra permissions are required.
  • Web: There is no real browser crash detection. The web implementation only simulates the recovery flow with local storage.

Documentation

API

Capacitor API for recovered WebView crash and restart detection.

getPendingCrashInfo()

getPendingCrashInfo() => Promise<PendingCrashInfoResult>

Returns the pending native crash or restart marker, if one exists.

Returns: Promise<PendingCrashInfoResult>


clearPendingCrashInfo()

clearPendingCrashInfo() => Promise<void>

Clears the stored marker after the app has handled recovery.


simulateCrashRecovery()

simulateCrashRecovery() => Promise<PendingCrashInfoResult>

Creates a fake crash marker so recovery flows can be tested locally.

Returns: Promise<PendingCrashInfoResult>


restartWebView()

restartWebView() => Promise<PendingCrashInfoResult>

Stores a manual restart marker and asks native code to create a fresh WebView.

On Android this recreates the host Activity. On iOS this rebuilds the Capacitor bridge view so a new WKWebView instance is created instead of reloading the current page.

Returns: Promise<PendingCrashInfoResult>


addListener('webViewRestoredAfterCrash' | 'webViewRestoredAfterRestart', ...)

addListener(eventName: 'webViewRestoredAfterCrash' | 'webViewRestoredAfterRestart', listenerFunc: (info: WebViewCrashInfo) => void) => Promise<PluginListenerHandle>

Fires after a new JavaScript runtime attaches a listener and a matching marker is still pending.

ParamType
eventName'webViewRestoredAfterCrash' | 'webViewRestoredAfterRestart'
listenerFunc(info: WebViewCrashInfo) => void

Returns: Promise<PluginListenerHandle>


removeAllListeners()

removeAllListeners() => Promise<void>

Removes all plugin listeners.


Interfaces

PendingCrashInfoResult

Pending crash or restart marker returned to JavaScript.

PropTypeDescription
valueWebViewCrashInfo | nullStored crash or restart metadata, or null when no marker is pending.

WebViewCrashInfo

Metadata captured natively after the previous WebView process died or was restarted.

PropTypeDescription
platformWebViewCrashPlatformPlatform that detected and stored the marker.
timestampnumberUnix timestamp in milliseconds for when the marker was written.
timestampISOstringISO-8601 version of timestamp.
reasonWebViewCrashReasonPlatform-specific reason for the crash or restart marker.
urlstringLast known WebView URL when the marker was written.
didCrashbooleanAndroid-only hint from RenderProcessGoneDetail.didCrash().
rendererPriorityAtExitnumberAndroid-only renderer priority reported at exit.
appStateWebViewCrashAppStateiOS-only application state captured when the marker was written.

PluginListenerHandle

PropType
remove() => Promise<void>

Type Aliases

WebViewCrashPlatform

Platform that produced the stored marker.

'android' | 'ios' | 'web'

WebViewCrashReason

Native reason reported for the previous WebView failure or restart.

'renderProcessGone' | 'webContentProcessDidTerminate' | 'periodicRestart' | 'manualRestart' | 'simulated'

WebViewCrashAppState

Best-effort application state captured on iOS when the WebView process died.

'active' | 'inactive' | 'background' | 'unknown'

版本与发布记录

@capgo/capacitor-webview-crash 共发布 18 个版本,首次发布于 2026/04/29;过去 12 个月发布了 18 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.1.13latest2026/10/1525 天前
8.1.122026/10/1327 天前
8.1.112026/10/081 个月前
8.1.102026/09/161 个月前
8.1.92026/09/042 个月前
8.1.82026/09/032 个月前

其他发布通道:lts-v7 → 7.1.0。

关于 @capgo/capacitor-webview-crash 的常见问题

@capgo/capacitor-webview-crash 是什么?

Capacitor plugin for detecting WebView crash recovery and restarting long-running WebViews natively.

如何安装 @capgo/capacitor-webview-crash?

在项目目录运行 npm install @capgo/capacitor-webview-crash,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(>=8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@capgo/capacitor-webview-crash 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@capgo/capacitor-webview-crash 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@capgo/capacitor-webview-crash 可以商用吗?

@capgo/capacitor-webview-crash 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@capgo/capacitor-webview-crash 的下载量有多少?

根据 npm 官方下载统计,@capgo/capacitor-webview-crash 在 2026/10/09 – 2026/11/07 这 30 天内被下载 72,537 次,日均约 2,418 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@capgo/capacitor-webview-crash 还在维护吗?

最新版本 8.1.13 发布于 2026/10/15(25 天前);至今共发布 18 个版本,其中过去 12 个月发布了 18 个;首次发布于 2026/04/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。