前端错误日志上报为啥有时候收不到数据?
我在项目里用 navigator.sendBeacon 上报错误日志,本地测试都能发出去,但线上偶尔就收不到,特别是一些白屏或者卡死的场景。是不是这些情况下 sendBeacon 也失效了?
我试过换成 new Image().src 的方式,但还是不稳定。有没有更可靠的上报方案?比如在页面 unload 的时候强制发送?
window.addEventListener('error', (e) => {
const log = { msg: e.message, file: e.filename, line: e.lineno };
navigator.sendBeacon('/log', JSON.stringify(log));
});
暂无解答