背景
从 chrome 137 版本开始,新增支持了在崩溃上报 API(Reporting API)中,上报 JS 堆栈信息,注意仅适用于由页面无响应(unresponsive)导致的崩溃,详情链接
上报条件
用户 chrome 版本 ≥ 137 外,还需满足下面 3 个条件:
- 崩溃原因是 unresponsive
- 站点文档返回头包含 document-policy,值为 include-js-call-stacks-in-crash-reports
- 可以从崩溃的文档中恢复调用堆栈(说明当崩溃原因为 unresponsive 时,也不一定都能上报堆栈信息)
接入方式
步骤 1:接入 Reporting API, 可以参考这篇掘金文章 捕捉异常 🔍 前端页面崩溃监控技术方案 - 掘金 的 Reporting API 上报 章节
步骤 2:这是 Chrome 137 版本新增功能, 需要配置 Document-Policy HTTP 返回头,值为 include-js-call-stacks-in-crash-reports,举例:
- 在 ngnix 中配置,在 server 或 location 块中添加:
add_header Document-Policy "include-js-call-stacks-in-crash-reports" always;
- 在 node(nestjs) 服务配置,在中间件中设置响应头:
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
@Injectable()
export class DocumentPolicyMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction) {
res.setHeader('Document-Policy', 'include-js-call-stacks-in-crash-reports');
next();
}
}
效果展示
附录
相关讨论:issues.chromium.org/issues/4026…
相关文档: