分析
¥Analytics
Next.js 内置了对测量和报告性能指标的支持。你可以使用 useReportWebVitals
钩子自行管理报告,或者 Vercel 提供 托管服务 来自动为你收集和可视化指标。
¥Next.js has built-in support for measuring and reporting performance metrics. You can either use the useReportWebVitals
hook to manage reporting yourself, or alternatively, Vercel provides a managed service to automatically collect and visualize metrics for you.
客户端检测
¥Client Instrumentation
对于更高级的分析和监控需求,Next.js 提供了一个 instrumentation-client.js|ts
文件,它会在应用的前端代码开始执行之前运行。这非常适合设置全局分析、错误跟踪或性能监控工具。
¥For more advanced analytics and monitoring needs, Next.js provides a instrumentation-client.js|ts
file that runs before your application's frontend code starts executing. This is ideal for setting up global analytics, error tracking, or performance monitoring tools.
要使用它,请在应用的根目录中创建一个 instrumentation-client.js
或 instrumentation-client.ts
文件:
¥To use it, create an instrumentation-client.js
or instrumentation-client.ts
file in your application's root directory:
构建你自己的
¥Build Your Own
查看 API 参考 了解更多信息。
¥View the API Reference for more information.
网络生命体
¥Web Vitals
网络生命体 是一组有用的指标,旨在捕捉网页的用户体验。以下 Web Vitals 均包含在内:
¥Web Vitals are a set of useful metrics that aim to capture the user experience of a web page. The following web vitals are all included:
到第一个字节的时间(TTFB)
¥Time to First Byte (TTFB)
第一次内容丰富的绘画(FCP)
¥First Contentful Paint (FCP)
最大的内容涂料(LCP)
¥Largest Contentful Paint (LCP)
第一个输入延迟(最终检测结果)
¥First Input Delay (FID)
累积布局偏移(CLS)
¥Cumulative Layout Shift (CLS)
与 Next 绘画的交互(INP)
¥Interaction to Next Paint (INP)
你可以使用 name
属性处理这些指标的所有结果。
¥You can handle all the results of these metrics using the name
property.
自定义指标
¥Custom Metrics
除了上面列出的核心指标外,还有一些其他自定义指标用于衡量页面加载和渲染所需的时间:
¥In addition to the core metrics listed above, there are some additional custom metrics that measure the time it takes for the page to hydrate and render:
Next.js-hydration
:页面开始和完成数据同步所需的时间(以毫秒为单位)¥
Next.js-hydration
: Length of time it takes for the page to start and finish hydrating (in ms)Next.js-route-change-to-render
:页面在路由更改后开始渲染所需的时间(以毫秒为单位)¥
Next.js-route-change-to-render
: Length of time it takes for a page to start rendering after a route change (in ms)Next.js-render
:页面在路由更改后完成渲染所需的时间(以毫秒为单位)¥
Next.js-render
: Length of time it takes for a page to finish render after a route change (in ms)
你可以单独处理所有这些指标的结果:
¥You can handle all the results of these metrics separately:
这些指标适用于所有支持 用户计时 API 的浏览器。
¥These metrics work in all browsers that support the User Timing API.
将结果发送到外部系统
¥Sending results to external systems
你可以将结果发送到任何端点,以测量和跟踪站点上的真实用户性能。例如:
¥You can send results to any endpoint to measure and track real user performance on your site. For example:
需要了解:如果你使用 谷歌分析,则使用
id
值可以让你手动构建度量分布(以计算百分位数等)¥Good to know: If you use Google Analytics, using the
id
value can allow you to construct metric distributions manually (to calculate percentiles, etc.)
阅读有关 将结果发送至 Google Analytics 的更多信息。
¥Read more about sending results to Google Analytics.