
- Next.js - Home
- Next.js - Overview
- Next.js - Project Setup
- Next.js - Folder Structure
- Next.js - App Router
- Next.js - Page Router
- Next.js Features
- Next.js - Pages
- Next.js - Data Fetching
- Next.js - ISR
- Next.js - Static File Serving
- Next.js - Pre-Rendering
- Next.js - Partial Pre Rendering
- Next.js - Server Side Rendering
- Next.js - Client Side Rendering
- Next.js Routing
- Next.js - Routing
- Next.js - Nested Routing
- Next.js - Dynamic Routing
- Next.js - Parallel Routing
- Next.js - Imperative Routing
- Next.js - Shallow Routing
- Next.js - Intercepting Routes
- Next.js - Redirecting Routes
- Next.js - Navigation and Linking
- Next.js Configuration
- Next.js - TypeScript
- Next.js - Environment Variables
- Next.js - File Conventions
- Next.js - ESLint
- Next.js API & Backend
- Next.js - API Routes
- Next.js - Dynamic API Routes
- Next.js - Route Handlers
- Next.js - API MiddleWares
- Next.js - Response Helpers
- Next.js API Reference
- Next.js - CLI Commands
- Next.js - Functions
- Next.js - Directives
- Next.js - Components
- Next.js - Image Component
- Next.js - Font Component
- Next.js - Head Component
- Next.js - Form Component
- Next.js - Link Component
- Next.js - Script Component
- Next.js Styling & SEO
- Next.js - CSS Support
- Next.js - Global CSS Support
- Next.js - Meta Data
- Next.js Advanced Topics
- Next.js - Error Handling
- Next.js - Server Actions
- Next.js - Fast Refresh
- Next.js - Internationalization
- Next.js - Authentication
- Next.js - Session Management
- Next.js - Authorization
- Next.js - Caching
- Next.js - Data Caching
- Next.js - Router Caching
- Next.js - Full Route Caching
- Next.js - Request Memoization
- Next.js Performance Optimization
- Next.js - Optimizations
- Next.js - Image Optimization
- Next.js - Lazy Loading
- Next.js - Font Optimization
- Next.js - Video Optimization
- Next.js - Script Optimization
- Next.js - Memory Optimization
- Next.js - Using OpenTelemetry
- Next.js - Package Bundling Optimization
- Next.js Testing
- Next.js - Testing
- Next.js - Testing with Jest
- Next.js - Testing with Cypress
- Next.js - Testing with Vitest
- Next.js - Testing with Playwright
- Next.js Debugging & Deployment
- Next.js - Debugging
- Next.js - Deployment
- Next.js Useful Resources
- Next.js - Interview Questions
- Next.js - Quick Guide
- Next.js - Useful Resources
- Next.js - Discussion
Next.js - userReportWebVitals() Function
The userReportWebVitals() Function
The Next.js userReportWebVitals() function is used to report Web Vitals such as Time to First Byte (TTFB), First Contentful Paint (FCP), Largest Contentful Paint (LCP), etc. to analytics tools.
What are Web Vitals?
Web vitals are a set of metrics that measure the performance of a web page. These metrics help developers to understand the user experience and identify areas for improvement in website. The key Web Vitals include:
- FCP(First Contentful Paint): The time it takes for the first content to be rendered on the screen.
- TBT(Total Blocking Time): The total amount of time the page is blocked from responding to user input.
- LCP(Large Contentful Paint): The time it takes for the largest content element to be rendered on the screen.
- TTFB(Time to First Byte): The time it takes for the server to respond to the initial request.
Syntax
Following is syntax of userReportWebVitals() function:
import { useReportWebVitals } from 'next/web-vitals' function MyApp({ Component, pageProps }) { useReportWebVitals((metric) => { switch (metric.name) { case 'FCP': { // handle FCP results } case 'LCP': { // handle LCP results } // ... } }) return <Component {...pageProps} /> }
Parameters
The useReportWebVitals() function takes a metric object as its parameter. The object will have following properties:
- name: The name of the metric (e.g., FCP, LCP, etc.)
- id: Unique identifier for the metric in the context of the current page load
- delta: The difference between the current value and the previous value of the metric
- entries: An array of performance entries associated with the metric
- navigationType: Indicates the type of navigation that triggered the metric collection.
- rating: A qualitative rating of the metric value, providing an assessment of the performance.
- value: The actual value or duration of the performance entry, typically in milliseconds.
Return Value
The function does not return any value.
Example 1
The example below shows simple implementation of userReportWebVitals() function. We displayed the metric object on the page using useState hook:
import { useState } from 'react'; import { useReportWebVitals } from 'next/web-vitals'; export default function Home() { const [metric, setMetric] = useState(null); useReportWebVitals((metric) => { setMetric(metric); }); return ( <div> <h2>Welcome to the Next JS page!</h2> <p>{JSON.stringify(metric)}</p> </div> ); }
Output

Example 2
The following code snippet shows how to send the Web Vitals data to an analytics endpoint using the navigator.sendBeacon() method. If the sendBeacon() method is not available, it falls back to using the fetch() method:
useReportWebVitals((metric) => { const body = JSON.stringify(metric) const url = 'https://example.com/analytics' // Use `navigator.sendBeacon()` if available, falling back to `fetch()`. if (navigator.sendBeacon) { navigator.sendBeacon(url, body) } else { fetch(url, { body, method: 'POST', keepalive: true }) } })