@bbc/web-vitals

中文文档
2.6.0发布于 2026/01/16Apache-2.0ESM + CJS近 30 天下载 2611

Generic hook for reporting web vitals metrics to an endpoint

原始使用文档
文档目录12 节
  1. Metrics we report on
  2. Core Web-Vitals
  3. Other data (useful for categorising the web-vitals data rec…
  4. Installation
  5. Install yarn
  6. Props
  7. Usage
  8. When to use this utility
  9. When not to use this utility
  10. Contributing
  11. Code of Conduct
  12. License

Web-Vitals

The WebVitals utility provides a configurable hook which will collect device capability metrics and Web Vitals metrics for each page view, and then send those metrics to a reporting endpoint when the user leaves the page.

Metrics we report on

Core Web-Vitals

  • CLS (Cumulative Layout Shift)
  • LCP (Largest Contentful Paint)
  • FID (First Input Delay)

Other data (useful for categorising the web-vitals data received)

  • FCP (First Contentful Paint)

  • TTFB (Time To First Byte)

  • INP (Interaction to Next Paint)

  • Device Memory

  • Device CPU

  • Effective Connection Type

Installation

npm install @bbc/web-vitals --save

Install yarn

The Web Vitals project uses yarn for package management. It is recommended to install yarn through the npm package manager, which comes bundled with Node.js when you install it on your system. To install Yarn, run this command:

npm install --global yarn

Props

ArgumentTypeRequiredDefaultExample
enabledBooleanNofalse{ enabled: true }
reportingEndpointStringYesN/A{ reportingEndpoint: 'https://url.to.report.to/analytics' }
loggerCallbackFunctionNo() => {}{ loggerCallback: (error) => console.error(error) }
sampleRateIntegerNo100{ sampleRate: 5 }
reportParamsObjectNoundefined{ reportParams: { pageType: 'STY' }
fetchFunctionNowindow.fetchAllows you to provide a fetch ponyfill if you support browsers that do not have a native fetch function

Usage

The hook can be configured in a number of ways:

  • enabled - setting this to true will enable sending these metrics to the reporting endpoint. This is so that the reporting functionality can be turned on only when users have agreed to the appropriate privacy and tracking settings where appropriate.
  • reportingEndpoint - the endpoint you wish to send your metrics to
  • loggerCallback - a function that will be called whenever an attempt to send the metrics fails.
  • sampleRate - providing a number between 1-100 will set the percentage of sampling, to prevent overloading the backend reporting server. e.g. a sampleRate of 5 will mean that metrics for 5% of requests will be sent to the reporting server.
  • reportParams - an object that will define query string parameters appended to the reporting endpoint. As an example, providing { pageType: 'STY' } will cause ?pageType=STY to be appended to the reporting endpoint.
import useWebVitals from "@bbc/web-vitals";
import logger from "./logger";

const Page = props => {
  const enabled = isOnClient() && hasUserAcceptedTracking();
  useWebVitals({
    enabled,
    reportingEndpoint: 'https://url.to.report.to/analytics',
    loggerCallback = logger.error,
    sampleRate: 50,
    reportParams: { pageType: 'STY' },
  });

  return (
    <PageComponents />
  );
};

When to use this utility

This utility should ideally be used as close to the top-level of your app as is practical. You should follow due diligence in regards to checking if your site visitor needs to accept any personalisation or tracking permissions under relevant local laws, and use this in determining the value of the enabled prop.

useEffect hooks are only run client-side, but when using this hook in combination with Server Side Rendering, you may wish to also include a check of whether the code is running client-side in order to ensure that you do not get inconsistencies between your server's capabilities and the user's.

When not to use this utility

In non-page-level components, especially where these components may be added multiple times to a page.

In situations where users have not provided permission for the relevant personalisation or tracking permissions that may be required under relevant local laws.

Contributing

Web Vitals is completely open source. We are grateful for any contributions, whether they be new utilities, bug fixes or general improvements. Please see our primary contributing guide which can be found at the root of the Web Vitals repository.

Code of Conduct

We welcome feedback and help on this work. By participating in this project, you agree to abide by the code of conduct. Please take a moment to read it.

License

Web-Vitals is Apache 2.0 licensed.

版本与发布记录

@bbc/web-vitals 共发布 49 个版本,首次发布于 2020/10/07;过去 12 个月发布了 1 个版本。

版本发布日期距今
2.6.0latest2026/01/169 个月前
2.5.32025/02/111.7 年前
2.5.22025/02/101.7 年前
2.5.12025/02/101.7 年前
2.5.02025/02/101.7 年前
2.4.12025/01/171.8 年前

关于 @bbc/web-vitals 的常见问题

@bbc/web-vitals 是什么?

Generic hook for reporting web vitals metrics to an endpoint

如何安装 @bbc/web-vitals?

在项目目录运行 npm install @bbc/web-vitals,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^17.0.0 || ^18.0.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bbc/web-vitals 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bbc__web-vitals 是否存在。

@bbc/web-vitals 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bbc/web-vitals 可以商用吗?

@bbc/web-vitals 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bbc/web-vitals 的下载量有多少?

根据 npm 官方下载统计,@bbc/web-vitals 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,611 次,日均约 87 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bbc/web-vitals 还在维护吗?

最新版本 2.6.0 发布于 2026/01/16(9 个月前);至今共发布 49 个版本,其中过去 12 个月发布了 1 个;首次发布于 2020/10/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。