__CAPGO_KEEP_8__
复制一个包含安装步骤和本插件的完整 Markdown 指南的配置提示。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-webview-crash`
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/webview-crash/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.
安装
标题:安装npm install @capgo/capacitor-webview-crashnpx cap sync导入
标题:导入import { WebViewCrash } from '@capgo/capacitor-webview-crash';推荐恢复流程
标题:推荐恢复流程尽早在应用启动时附加监听器,以便恢复的运行时可以在用户继续浏览之前做出反应:
import { WebViewCrash } from '@capgo/capacitor-webview-crash';
await WebViewCrash.addListener('webViewRestoredAfterCrash', async (info) => { console.log('Recovered after a WebView crash', info);
// Rehydrate critical state, reopen the correct screen, or prompt the user to retry. 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();// Note: the listener callback may have already cleared the pending marker.if (pending.value) { console.log('Pending crash or restart marker', pending.value);}原生自动重启
原生自动重启在 capacitor.config.ts 这样即使JavaScript运行时已崩溃或未加载,原生code仍然保持不变:
import type { CapacitorConfig } from '@capacitor/cli';import type { WebViewCrashPluginConfig } from '@capgo/capacitor-webview-crash';
const webViewCrash: WebViewCrashPluginConfig = { // Enabled by default. Keep this on for long-running apps. 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;使用 restartIntervalMs 始终开启的应用程序,用户可能会在几天内保持相同的WebView打开: restartCron 使用 0 3 * * * 墙时钟重启,如 reason: 'periodicRestart', then Android recreates the host activity and iOS rebuilds the Capacitor bridge view so a new WKWebView is created from native code.
选择一个产品可以容忍的时间间隔或cron调度。 restartCron 支持 *, 列表、范围和步长。请勿同时配置两个调度:当 restartCron 设置为true且 restartIntervalMs 大于 0时,native初始化会抛出致命配置错误。重启会创建一个新的JavaScript运行时,因此在使用激进调度之前,请务必保存排队事件、未保存的表单状态和当前导航状态。
手动native重启
标题:手动native重启调用 restartWebView() 当当前JavaScript运行时决定native WebView应该被替换时,例如在内存密集型工作流或进入长时间未attended会话之前:
await WebViewCrash.restartWebView();该方法写入一个待处理标记 reason: 'manualRestart'解决当前调用后,native code 会创建一个新的 WebView。Android 会重新创建宿主活动。iOS 会重建 Capacitor 桥视图以创建一个新的 WKWebView 而不是重新加载当前页面。
API 概述
标题:“API 概述”getPendingCrashInfo
标题:“getPendingCrashInfo”返回存储的native崩溃或重启标记,或者 null 当没有待处理的标记时。
const pending = await WebViewCrash.getPendingCrashInfo();if (pending.value) { console.log(pending.value.platform, pending.value.reason);}clearPendingCrashInfo
标题:“clearPendingCrashInfo”清除存储的标记后,完成恢复处理。
await WebViewCrash.clearPendingCrashInfo();simulateCrashRecovery
标题:“simulateCrashRecovery”创建一个虚拟的崩溃标记,以便 QA 和本地调试可以在不崩溃真实 WebView 的情况下演练恢复路径。
const simulated = await WebViewCrash.simulateCrashRecovery();console.log(simulated.value);restartWebView
标题:重新启动 WebView存储一个手动重新启动标记并要求本地 code 创建一个新的 WebView。
await WebViewCrash.restartWebView();平台说明
标题:平台说明- Android 将崩溃元数据存储在
onRenderProcessGone包括didCrash和rendererPriorityAtExit当平台提供它们时。 - iOS 将崩溃元数据存储在
webViewWebContentProcessDidTerminateand adds the current application state when available. - 手动和预定重启创建一个新的 WebView。Android 重建宿主活动;iOS 重建Capacitor 桥视图.
- 预定重启使用
reason: 'periodicRestart';手动重启使用reason: 'manualRestart'. - Web 不检测真实的渲染器崩溃。Web 实现仅模拟本地存储的行为.
类型参考
类型参考PendingCrashInfoResult
待处理崩溃信息结果export interface PendingCrashInfoResult { /** * Stored crash or restart metadata, or `null` when no marker is pending. */ value: WebViewCrashInfo | null;}WebViewCrashPluginConfig
WebView 崩溃插件配置export interface WebViewCrashPluginConfig { /** * Restart the WebView from native code when the renderer process dies. * * @default true */ restartOnCrash?: boolean;
/** * Fixed native interval, in milliseconds, for proactively replacing long-running WebViews. * * Set to `0` to disable interval restarts. Do not combine an active interval * with `restartCron`; native initialization fails fast when both schedules are configured. * * @default 0 */ restartIntervalMs?: number;
/** * Cron schedule for proactively replacing long-running WebViews. * * Uses standard 5-field cron syntax in the device local timezone: * `minute hour day-of-month month day-of-week`. * * Examples: * - `0 3 * * *` restarts every day at 03:00. * - `0,30 * * * *` restarts every 30 minutes. * * Do not combine this with an active `restartIntervalMs`; native initialization * fails fast when both schedules are configured. */ restartCron?: string;
/** * Delay, in milliseconds, before restarting after a crash. * * @default 0 */ restartAfterCrashDelayMs?: number;}WebViewCrashInfo
WebView 崩溃信息export interface WebViewCrashInfo { /** * Platform that detected and stored the marker. */ platform: WebViewCrashPlatform;
/** * Unix timestamp in milliseconds for when the marker was written. */ timestamp: number;
/** * ISO-8601 version of `timestamp`. */ timestampISO: string;
/** * Platform-specific reason for the crash or restart marker. */ reason: WebViewCrashReason;
/** * Last known WebView URL when the marker was written. */ url?: string;
/** * Android-only hint from `RenderProcessGoneDetail.didCrash()`. */ didCrash?: boolean;
/** * Android-only renderer priority reported at exit. */ rendererPriorityAtExit?: number;
/** * iOS-only application state captured when the WebView process died. */ appState?: WebViewCrashAppState;}WebViewCrashPlatform
WebViewCrashPlatformexport type WebViewCrashPlatform = 'android' | 'ios' | 'web';WebViewCrashReason
WebViewCrashReasonexport type WebViewCrashReason = | 'renderProcessGone' | 'webContentProcessDidTerminate' | 'periodicRestart' | 'manualRestart' | 'simulated';WebViewCrashAppState
WebViewCrashAppStateexport type WebViewCrashAppState = 'active' | 'inactive' | 'background' | 'unknown';真实来源
真实来源本页面是由插件生成的。 src/definitions.ts当公共API在上游发生变化时,请重新同步。
继续从开始入门
继续从 Getting Started 开始如果您正在使用 Getting Started 来规划原生媒体和界面行为,连接它与 使用 @capgo/capacitor-webview-crash 为原生能力在使用 @capgo/capacitor-webview-crash 中 使用 @capgo/capacitor-live-activities 为原生能力在使用 @capgo/capacitor-live-activities 中 @capgo/capacitor-live-activities 为在 @capgo/capacitor-live-activities 中实现的细节 使用 @capgo/capacitor-video-player 为原生能力在使用 @capgo/capacitor-video-player 中 @capgo/capacitor-视频播放器 为 @capgo/capacitor-视频播放器 的实现细节。