Capítulo 187 de 456
Hook to capture and report Core Web Vitals (TTFB, FCP, LCP, FID, CLS, INP), typically forwarding them to an analytics service. Keep the callback reference stable to avoid duplicate reporting.
callback com cada métrica disponível; requer 'use client'.{ id, name, delta, entries, navigationType, rating, value }."good" | "needs-improvement" | "poor".'use client'
import { useReportWebVitals } from 'next/web-vitals'
const logWebVitals = (metric) => console.log(metric)
export function WebVitals() {
useReportWebVitals(logWebVitals)
return null
}
import { WebVitals } from './_components/web-vitals'
export default function Layout({ children }) {
return (
<html><body><WebVitals />{children}</body></html>
)
}
'use client'.| Propriedade | Descrição |
|---|---|
id | Identificador único da métrica no carregamento atual |
name | Nome da métrica (TTFB, FCP, LCP, FID, CLS, INP) |
delta | Diferença entre valor atual e anterior |
entries | Array de PerformanceEntry associadas |
navigationType | "navigate", "reload", "prerender", "back-forward", "back-forward-cache", "restore" |
rating | "good", "needs-improvement", "poor" |
value | Valor/duração da métrica |
navigator.sendBeacon() com fallback para fetch(..., { keepalive: true }) ao reportar para endpoint externo.metric.name num switch para tratar cada Web Vital individualmente.metric.id para construir distribuições/percentis.