Skip to content

NextJS中文文档 - Instrumentation Client

instrumentation-client.js|ts 文件允许你添加在应用程序前端代码开始执行之前运行的监控和分析代码。这对于设置性能跟踪、错误监控或任何其他客户端可观测性工具非常有用。

要使用它,请将文件放在应用程序的根目录中或 src 文件夹内。

用法

服务器端检测不同,你不需要导出任何特定函数。你可以直接在文件中编写监控代码:

ts
// 设置性能监控
performance.mark('app-init')

// 初始化分析
console.log('Analytics initialized')

// 设置错误跟踪
window.addEventListener('error', (event) => {
  // 发送到错误跟踪服务
  reportError(event.error)
})
js
// 设置性能监控
performance.mark('app-init')

// 初始化分析
console.log('Analytics initialized')

// 设置错误跟踪
window.addEventListener('error', (event) => {
  // 发送到错误跟踪服务
  reportError(event.error)
})

版本历史

版本变更
v15.3引入 instrumentation-client

🎉有任何问题,欢迎联系我

WeChat QR Code
WeChat
QQ QR Code
QQ

赣ICP备2023003243号