Skip to content
Styling & OptimizationChapter 30 / 34Advanced5 min read

Performance & Core Web Vitals

Next.js performance core web vitals (LCP, INP, CLS) ko measure aur improve karna, useReportWebVitals aur bundle analyzer ke saath, Roman Urdu mein.

On this page

1. Performance & Core Web Vitals kya hain?

MetricKya naapta haiAchha score
LCP (Largest Contentful Paint)Sab se bara element kitni der mein dikha2.5 second se kam
INP (Interaction to Next Paint)Click ya type ke baad screen kitni jaldi update hui200 ms se kam
CLS (Cumulative Layout Shift)Content load hote hue kitna khiska0.1 se kam

2. Kyun zaroori hai?

Slow site par users chale jate hain, aur Google ranking mein bhi page experience ek signal hai. Next.js bohot kuch default mein optimize karta hai, lekin galat choices se yeh faida khatam ho sakta hai.

Andaze se optimizeMeasure kar ke optimize
Lagta hai site slow haiReal users ka LCP, INP, CLS data
Har cheez Client ComponentServer Components, kam JavaScript
Bari library poori bundle meinAnalyzer se bhaari packages pakro
Images aur fonts bina optimizenext/image aur next/font

3. Kahan use hota hai?

ProblemAsarNext.js tool
Bari hero imageLCP kharabnext/image aur preload
Font swap par text khisknaCLS kharabnext/font
Bhaari client JavaScriptINP kharabServer Components, next/dynamic
Slow data fetchLCP kharabStreaming, Suspense, "use cache"
Third party scriptsINP aur LCP kharabnext/script ke saath strategy

4. Kaise kaam karta hai?

Performance improve karne ka loop
Real users se Web Vitals collect karo
Sab se kharab metric aur page dhoondo
Bundle analyzer ya DevTools se wajah pakro
Fix lagao (image, font, client JS, caching)
Production build par dobara measure karo

Development mode slow hota hai aur us ke numbers asal nahi hote. Hamesha next build aur next start ke baad, ya production par measure karein.

5. Code example

Real user metrics: useReportWebVitals

tsx
"use client";

import { useReportWebVitals } from "next/web-vitals";

type ReportCallback = Parameters<typeof useReportWebVitals>[0];

const sendToAnalytics: ReportCallback = (metric) => {
  const body = JSON.stringify({ name: metric.name, value: metric.value, rating: metric.rating });
  navigator.sendBeacon("/api/vitals", body);
};

export function WebVitals() {
  useReportWebVitals(sendToAnalytics);
  return null;
}

Is component ko root layout ke body mein WebVitals ki tarah render kar do.

Heavy component ko lazy load karna

tsx
"use client";

import dynamic from "next/dynamic";

const Chart = dynamic(() => import("./chart"), {
  loading: () => <p>Chart load ho raha hai...</p>,
});

export function Dashboard() {
  return <Chart />;
}

Bundle analyze karna

bash
npx next analyze

Turbopack (Next.js 16.1+) ka built-in analyzer route aur client ya server ke hisaab se modules dikhata hai. Webpack wala @next/bundle-analyzer plugin bhi abhi chalta hai:

ts
import type { NextConfig } from "next";
import bundleAnalyzer from "@next/bundle-analyzer";

const withBundleAnalyzer = bundleAnalyzer({ enabled: process.env.ANALYZE === "true" });

const nextConfig: NextConfig = {};

export default withBundleAnalyzer(nextConfig);
bash
ANALYZE=true npx next build --webpack

Code kaise kaam kar raha hai?

CheezMatlab
useReportWebVitalsHar metric (LCP, INP, CLS, FCP, TTFB) ready hone par callback chalata hai
Callback bahar defineFunction reference stable rehta hai, duplicate reports nahi
sendBeaconPage band hote hue bhi data bhej deta hai
dynamic()Chart ka code alag chunk mein, zaroorat par load
next analyzeTurbopack bundle ka interactive treemap
--webpack@next/bundle-analyzer webpack plugin hai, is liye webpack build chahiye

6. Common mistakes

Galati: next dev mein performance naapna

Why? Development mode mein extra checks aur unoptimized code hota hai, numbers production se bohot bure aate hain.

Behtar: next build aur next start chala kar Lighthouse ya real user data dekho.

Galati: Poore page ko use client bana dena

Why? Saara component tree aur us ki libraries browser bundle mein chali jati hain, INP aur load time dono bigarte hain.

Behtar: use client sirf interactive leaf components par lagao, baqi Server Components rakho.

Galati: Sirf Lighthouse score par bharosa

Why? Lighthouse lab test hai ek machine par, real users ke slow phones aur network alag hote hain, aur INP lab mein naapa hi nahi jata.

Behtar: useReportWebVitals ya Vercel Speed Insights se field data bhi collect karo.

Galati: Heavy data transform client par karna

Why? Markdown parsing ya syntax highlighting jaisi libraries client bundle bara karti hain halanke output sirf HTML hai.

Behtar: Aisa kaam Server Component mein karo, client ko sirf rendered markup bhejo.

Interview tip

"INP ne FID ki jagah kyun li?" FID sirf pehle interaction ki delay naapta tha. INP poore visit ke interactions mein se sab se slow ke qareeb wala naapta hai, is liye real responsiveness ki behtar tasveer deta hai.

7. Pehle aur baad mein kya seekhein?

Final quick revision

PointDetail
LCPLoading, 2.5s se kam
INPResponsiveness, 200ms se kam
CLSStability, 0.1 se kam
MeasureuseReportWebVitals from next/web-vitals
Bundle checknext analyze ya @next/bundle-analyzer with --webpack
Kam JSServer Components aur next/dynamic