# Core Web Vitals（LCP、INP 与 CLS）

> LCP、INP 与 CLS 各测量什么、第 75 百分位上 2.5 s、200 ms 与 0.1 的阈值、哪些引擎暴露各条目类型，以及 PWA 对每项的可用手段。

Core Web Vitals 用 Largest Contentful Paint 衡量加载，用 Interaction to Next Paint 衡量响应性，
用 Cumulative Layout Shift 衡量视觉稳定性。Google 的工具用这三项现场指标判断页面体验。
真实访问的第 75 百分位在三项上都达到「良好」，页面才算通过。开发 PWA 时，可以调整 Service Worker 策略、
拆分任务并预留布局空间，改善这些指标。

## 工作原理

各指标以 `PerformanceEntry` 类型提供，由 `PerformanceObserver` 读取，在一次访问内汇总。`web-vitals` 库或 Chrome User Experience Report（CrUX）负责上报现场数据。阈值来自 Chrome 团队对用户感知质量的校准（[Web Vitals](https://web.dev/articles/vitals)，web.dev）。

| 指标 | 测量内容 | 良好 | 待改进 | 差 | 条目类型 |
|---|---|---|---|---|---|
| LCP | 视口内最大图片或文本块相对导航开始的渲染时间 | ≤ 2.5 s | ≤ 4.0 s | > 4.0 s | `largest-contentful-paint`（Chrome 77、Firefox 122、Safari 26.2） |
| INP | 整次访问中最慢交互从输入到下一次绘制的延迟，交互很多的页面按高百分位截断 | ≤ 200 ms | ≤ 500 ms | > 500 ms | 带 `interactionId` 的 `event`（Chrome 96、Firefox 144、Safari 26.2） |
| CLS | 最差会话窗口内意外布局偏移得分之和 | ≤ 0.1 | ≤ 0.25 | > 0.25 | `layout-shift`（仅 Chromium） |

INP 于 2024 年 3 月取代 First Input Delay 成为响应性指标（[Interaction to Next Paint](https://web.dev/articles/inp)，web.dev）；FID 只测首次交互的输入延迟，INP 则包含处理时间和其后那一帧的绘制。阈值按页面访问的第 75 百分位计算，移动端与桌面端分开。

### 现场数据与实验室数据

INP 与 CLS 在整次访问中累积，所以 Lighthouse 或 WebPageTest 的单次脚本加载无法复现它们；实验室工具用 Total Blocking Time 和加载期 CLS 作为替代。CrUX 为流量足够的源发布现场分布，`web-vitals` 库通过 `onLCP()`、`onINP()`、`onCLS()` 回调给出页面自己的数字。可以用实验室数据排查原因，再用现场数据评估真实访问。

### 条目类型的支持情况

在现场测量取决于引擎是否暴露对应条目：`largest-contentful-paint` 在 Chrome 77、Firefox 122、Safari 26.2；带 `interactionId` 的 `event` 条目在 Chrome 96、Firefox 144、Safari 26.2；`layout-shift` 仅 Chromium（BCD `api.LargestContentfulPaint`、`api.PerformanceEventTiming.interactionId`、`api.LayoutShift`）。CrUX 以及搜索工具据此报告的现场数字只从 Chrome 采集。

### PWA 能控制的手段

- 改善 LCP 时，可以在 Service Worker 中采用缓存优先或 stale-while-revalidate，让重复访问直接从 Cache Storage 取得外壳和主图，省去关键路径上的网络请求；见[预缓存](/zh/reference/performance/precaching/)。首次访问时，给 LCP 图片加上 `fetchpriority="high"`，首屏图片不用懒加载，并让服务器在 HTML 中直接提供图片 URL，不等脚本再提供。
- 主线程长任务会推迟下一次绘制，影响 INP。用 `scheduler.yield()` 或 `setTimeout` 分块拆开工作，把解析和 hydration 移出交互路径，让事件处理函数保持短小；缩减启动时执行的代码量见[代码拆分](/zh/reference/performance/code-splitting/)。
- 要降低 CLS，先为内容预留空间。图片和嵌入内容设置 `width`、`height` 或 `aspect-ratio`，延迟出现的横幅预留固定高度。字体使用 `font-display: optional` 或度量匹配的后备字体，避免替换 Web 字体时文本重排；见[字体与图片优化](/zh/reference/performance/fonts-images/)。

## 实测行为

Chrome 无需额外的库就能读取这三种条目类型。示例代码在浏览器报告指标时打印结果；引擎缺少对应类型时按「不支持」处理，例如 Chromium 之外的 `layout-shift`。

```js
function observe(type, handler) {
  if (!('PerformanceObserver' in window)
      || !PerformanceObserver.supportedEntryTypes.includes(type)) {
    return false; // 这里不支持该条目类型：这项指标不上报
  }
  new PerformanceObserver((list) => list.getEntries().forEach(handler)).observe({ type, buffered: true });
  return true;
}

observe('largest-contentful-paint', (e) => console.log('LCP 候选', e.startTime, e.element));
observe('layout-shift', (e) => { if (!e.hadRecentInput) console.log('偏移', e.value, e.sources); });
observe('event', (e) => { if (e.interactionId) console.log('交互', e.name, e.duration); });
```

`largest-contentful-paint` 会随着更大的候选元素渲染而多次触发；首次交互或切换标签页之前的最后一条就是 LCP。`layout-shift` 条目带 `hadRecentInput`，用于排除用户输入后 500 ms 内的偏移，`sources` 则列出发生偏移的节点。

:::note
Chrome 129 为 DevTools 的 Performance 面板加入了 Core Web Vitals 的实时观测。面板打开时显示本地的 LCP 和 CLS，与页面交互后会出现 INP。面板还可以显示 Chrome UX Report 的现场数据用于对比，需要在 **Field data** > **Set up** 中启用（[What's New in DevTools (Chrome 129)](https://developer.chrome.com/blog/new-in-devtools-129)，developer.chrome.com）。现场指标可以在源和 URL 两种数据之间切换（数据可用时）（[Performance panel reference](https://developer.chrome.com/docs/devtools/performance/reference)，developer.chrome.com）。
:::

## 另请参阅

- [Web Vitals](https://web.dev/articles/vitals)（web.dev）
- [Interaction to Next Paint (INP)](https://web.dev/articles/inp)（web.dev）
- [Largest Contentful Paint API](https://w3c.github.io/largest-contentful-paint/)（w3.org）
- [预缓存策略](/zh/reference/performance/precaching/)
- [用动态 import() 做代码拆分](/zh/reference/performance/code-splitting/)
- [字体与图片优化](/zh/reference/performance/fonts-images/)
- [Lighthouse 与已移除的 PWA 类别](/zh/reference/performance/lighthouse/)