跳过内容

WebView Crash

GitHub

您可以使用我们的 AI 助手设置来安装插件。将 Capgo 技能添加到您的 AI 工具中,使用以下命令:

终端窗口
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

然后使用以下提示:

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

如果您更喜欢手动设置,请运行以下命令安装插件,并按照以下平台特定的说明进行操作:

终端窗口
npm install @capgo/capacitor-webview-crash
npx 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);
}

原生自动重启

Native 自动重启

设置重启选项在 capacitor.config.ts 这样决定就留在native code即使JavaScript运行时已经崩溃或尚未加载:

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 * * * 在设备本地时区的每日03:00重启。预定重启写入一个待处理标记, reason: 'periodicRestart'然后Android重新创建宿主活动,iOS重建Capacitor桥视图,所以一个新的 WKWebView 是从native code创建的。

选择一个间隔或cron计划你的产品可以容忍。 restartCron 支持 *,列表、范围和步骤。请勿同时配置两个调度:本机初始化会在配置错误时抛出致命错误。 restartCron 当 restartIntervalMs 大于 0。重启会创建一个新的 JavaScript 运行时,因此请在使用激进调度时保存排队事件、未保存的表单状态和当前导航状态。

手动本机重启

手动本机重启

调用 restartWebView() 当当前 JavaScript 运行时决定本机 WebView 应该被替换时,例如在内存密集型工作流之后或进入长时间未attended 会话之前:

await WebViewCrash.restartWebView();

写入一个等待标记 reason: 'manualRestart', resolves the current call, then asks native code to create a fresh WebView. Android recreates the host activity. iOS rebuilds the Capacitor bridge view so a new WKWebView Copy to clipboard

API概述

API概述

返回存储的本机崩溃或重启标记,或 null 当没有待处理的崩溃时。

const pending = await WebViewCrash.getPendingCrashInfo();
if (pending.value) {
console.log(pending.value.platform, pending.value.reason);
}

clearPendingCrashInfo

清除待处理的崩溃信息

模拟崩溃恢复

await WebViewCrash.clearPendingCrashInfo();

Section titled “__CAPGO_KEEP_0__ overview”

const simulated = await WebViewCrash.simulateCrashRecovery();
console.log(simulated.value);

存储手动重启标记并要求本机 code 创建一个新的 WebView。

await WebViewCrash.restartWebView();
  • Android 存储崩溃元数据从 onRenderProcessGone包括 didCrash 和 rendererPriorityAtExit context:Capgo 营销网站。角色:短 UI 标签或导航项。见于:page trust.astro。消息键 `and` (And)。
  • 当平台提供它们时。 webViewWebContentProcessDidTerminate iOS 存储崩溃元数据从
  • 手动和预定重启会创建一个新的 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

类型参考
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

类型参考
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

类型参考
export type WebViewCrashPlatform = 'android' | 'ios' | 'web';

WebViewCrashReason

WebViewCrashReason
export type WebViewCrashReason =
| 'renderProcessGone'
| 'webContentProcessDidTerminate'
| 'periodicRestart'
| 'manualRestart'
| 'simulated';

WebViewCrashAppState

WebViewCrashAppState
export type WebViewCrashAppState = 'active' | 'inactive' | 'background' | 'unknown';

真实来源

真实来源

本页面是根据插件生成的 src/definitions.ts当公共API在上游发生变化时,请重新运行同步。

继续从开始

继续从开始

如果您正在使用 开始使用 为原生媒体和界面行为做好准备,连接它 使用@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-video-player的实现细节