核心 Web 指标与监控
理解 LCP、INP 和 CLS,在真实用户中测量这些指标,并使用 Next.js 功能和可观测性工具保持生产应用的快速运行。
核心 Web 指标与监控 是 CoddyKit 上的免费 Next.js 15 Fullstack Web Apps 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Next.js 15 Fullstack Web Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Next.js 15 Fullstack Web Apps 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Are Core Web Vitals
Core Web Vitals are Google's user-centric performance metrics that influence both UX and SEO ranking. The three current metrics are:
- LCP — Largest Contentful Paint (loading)
- INP — Interaction to Next Paint (responsiveness)
- CLS — Cumulative Layout Shift (visual stability)
LCP: Loading Speed
LCP measures when the largest visible element (often a hero image or heading) finishes rendering. Aim for under 2.5s. Improve it with image optimization, priority loading, and server rendering.
import Image from 'next/image';
<Image src="/hero.jpg" alt="Hero" width={1200} height={600} priority />INP: Responsiveness
INP replaced FID. It measures the latency of user interactions across the whole visit. Aim for under 200ms. Long JavaScript tasks on the main thread hurt INP most.
CLS: Visual Stability
CLS quantifies unexpected layout shifts. Aim for under 0.1. Always set explicit width and height on images and reserve space for ads and embeds.
Lab vs Field Data
Lab data comes from a controlled run (Lighthouse). Field data (RUM) comes from real users on real devices. Field data is the source of truth for ranking; lab data is great for debugging.
Reporting with useReportWebVitals
Next.js exposes useReportWebVitals to collect real-user metrics and send them to your analytics endpoint.
'use client';
import { useReportWebVitals } from 'next/web-vitals';
export function Vitals() {
useReportWebVitals((metric) => {
navigator.sendBeacon('/api/vitals', JSON.stringify(metric));
});
return null;
}Bucketing a Metric
Classify a metric value into good / needs-improvement / poor. Pure logic you can run anywhere.
function rateLCP(ms) {
if (ms <= 2500) return 'good';
if (ms <= 4000) return 'needs-improvement';
return 'poor';
}
console.log(rateLCP(1800));
console.log(rateLCP(3200));
console.log(rateLCP(5000));Reducing Main-Thread Work
To improve INP, ship less JavaScript and defer non-critical work. Use dynamic imports to code-split heavy client components.
import dynamic from 'next/dynamic';
const Chart = dynamic(() => import('./Chart'), { ssr: false });Server Components Help
React Server Components render on the server and send no JavaScript to the client for that part of the tree, directly reducing bundle size and improving INP and LCP.
Production Monitoring Tools
Set up continuous observability:
- Vercel Speed Insights for built-in RUM.
- Vercel Analytics for traffic.
- Sentry / OpenTelemetry for errors and traces.
Alert when a metric regresses past its threshold.
Set a Performance Budget
Define budgets (e.g. LCP < 2.5s, JS < 170KB) and enforce them in CI so regressions fail the build before they reach users.
Quick Check
Which Core Web Vital measures the responsiveness of user interactions, and what is its target?
Recap
You learned to keep production fast:
- LCP (loading), INP (responsiveness), and CLS (stability) with their targets.
- The difference between lab and field data.
- Collecting RUM with
useReportWebVitals. - Reducing JS via code splitting and Server Components, plus monitoring and budgets.
常见问题解答
「核心 Web 指标与监控」课时是免费的吗?
是的 — 「核心 Web 指标与监控」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Next.js 15 Fullstack Web Apps 课程的其余内容,请升级到 CoddyKit PRO。 Next.js 15 Fullstack Web Apps 课程共包含 4 节课。
「核心 Web 指标与监控」这节课中我会学到什么?
理解 LCP、INP 和 CLS,在真实用户中测量这些指标,并使用 Next.js 功能和可观测性工具保持生产应用的快速运行。 你通过在浏览器中直接运行的动手代码来练习 Next.js 15 Fullstack Web Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Next.js 15 Fullstack Web Apps 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Next.js 15 Fullstack Web Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「核心 Web 指标与监控」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Next.js 15 Fullstack Web Apps 课中编写并运行代码吗?
能。每节 Next.js 15 Fullstack Web Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 部署到 Vercel 和 Netlify
- 图像与字体优化
- 捆绑包分析与性能审计
- 核心 Web 指标与监控