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?
| Metric | Kya naapta hai | Achha score |
|---|---|---|
| LCP (Largest Contentful Paint) | Sab se bara element kitni der mein dikha | 2.5 second se kam |
| INP (Interaction to Next Paint) | Click ya type ke baad screen kitni jaldi update hui | 200 ms se kam |
| CLS (Cumulative Layout Shift) | Content load hote hue kitna khiska | 0.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 optimize | Measure kar ke optimize |
|---|---|
| Lagta hai site slow hai | Real users ka LCP, INP, CLS data |
| Har cheez Client Component | Server Components, kam JavaScript |
| Bari library poori bundle mein | Analyzer se bhaari packages pakro |
| Images aur fonts bina optimize | next/image aur next/font |
3. Kahan use hota hai?
| Problem | Asar | Next.js tool |
|---|---|---|
| Bari hero image | LCP kharab | next/image aur preload |
| Font swap par text khiskna | CLS kharab | next/font |
| Bhaari client JavaScript | INP kharab | Server Components, next/dynamic |
| Slow data fetch | LCP kharab | Streaming, Suspense, "use cache" |
| Third party scripts | INP aur LCP kharab | next/script ke saath strategy |
4. Kaise kaam karta hai?
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
"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
"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
npx next analyzeTurbopack (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:
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);ANALYZE=true npx next build --webpackCode kaise kaam kar raha hai?
| Cheez | Matlab |
|---|---|
useReportWebVitals | Har metric (LCP, INP, CLS, FCP, TTFB) ready hone par callback chalata hai |
| Callback bahar define | Function reference stable rehta hai, duplicate reports nahi |
sendBeacon | Page band hote hue bhi data bhej deta hai |
dynamic() | Chart ka code alag chunk mein, zaroorat par load |
next analyze | Turbopack 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?
| Stage | Chapters |
|---|---|
| Pehle | Image Optimization, Font Optimization, Server & Client Components |
| Saath mein | Streaming & Suspense, Cache Components & use cache |
| Baad mein | next.config & Turbopack, Deployment |
Final quick revision
| Point | Detail |
|---|---|
| LCP | Loading, 2.5s se kam |
| INP | Responsiveness, 200ms se kam |
| CLS | Stability, 0.1 se kam |
| Measure | useReportWebVitals from next/web-vitals |
| Bundle check | next analyze ya @next/bundle-analyzer with --webpack |
| Kam JS | Server Components aur next/dynamic |