Skip to content
Styling & OptimizationChapter 26 / 34Beginner5 min read

Image Optimization

Next.js image component (next/image) se images resize, lazy load aur modern formats mein serve karna, Next.js 16 ke naye config defaults ke saath.

On this page

1. Image Optimization kya hai?

Aap ek bari 3000px ki JPG dete hain, aur Next.js mobile user ko chhoti WebP ya AVIF version bhejta hai. Yeh kaam Next.js ka built-in image optimizer karta hai.

2. Kyun zaroori hai?

Images aksar page ka sab se bhaari hissa hoti hain. Galat size ki image page slow karti hai aur bina width / height ke image layout ko jhatka deti hai.

Plain img tagnext/image
Har device ko same bari fileDevice ke hisaab se resized version
Purana JPG ya PNG formatWebP ya AVIF jahan browser support kare
Saari images foran downloadNeeche wali images lazy load
Load par layout shift (CLS)width aur height se jagah pehle reserve
Koi caching control nahiOptimized images cache hoti hain

3. Kahan use hota hai?

JagahExample
Hero bannerPage ki sab se bari image, LCP element
Product cardsE-commerce grid mein bohot si images
Blog thumbnailsCMS se aane wali remote images
AvatarsChhoti fixed size profile pictures
Background coverfill ke saath parent ko cover karti image

4. Kaise kaam karta hai?

Image request ka safar
Image component srcset aur sizes generate karta hai
Browser screen ke mutabiq ek URL choose karta hai
Request /_next/image par jati hai
Optimizer resize karta hai aur WebP ya AVIF banata hai
Result cache hota hai aur browser ko milta hai

Local image import karne par Next.js build time par uski width, height aur blur placeholder khud nikal leta hai. Remote image ke liye yeh values aapko deni padti hain, aur host ko next.config.ts mein allow karna padta hai.

5. Code example

Local image (static import)

tsx
import Image from "next/image";
import hero from "./hero.jpg";

export default function Home() {
  return (
    <Image
      src={hero}
      alt="Lahore ki raat ka manzar"
      placeholder="blur"
      preload
    />
  );
}

Remote image aur responsive fill

tsx
import Image from "next/image";

export function ProductCard({ url, name }: { url: string; name: string }) {
  return (
    <div className="relative aspect-square">
      <Image
        src={url}
        alt={name}
        fill
        sizes="(max-width: 768px) 100vw, 33vw"
        className="object-cover"
      />
    </div>
  );
}

next.config.ts mein images config

ts
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  images: {
    remotePatterns: [new URL("https://cdn.example.com/products/**")],
    localPatterns: [{ pathname: "/assets/**", search: "?v=2" }],
    qualities: [60, 75],
    minimumCacheTTL: 14400,
  },
};

export default nextConfig;

Code kaise kaam kar raha hai?

CheezMatlab
src={hero}Static import, size aur blur data build time par
preloadLCP image ko head mein preload karta hai (Next 16 mein priority ki jagah)
fillImage parent ko bhar deti hai, parent par position: relative zaroori
sizesBrowser ko batata hai image screen par kitni chouri hogi
remotePatternsSirf in URLs ki remote images optimize hongi
localPatternsLocal images ke path aur query string allow karta hai
qualitiesAllowed quality values ki list

Next.js 16 mein kya badla?

SettingNext.js 16 behaviour
qualitiesDefault ab [75], doosri quality value qareeb wali allowed value ban jati hai
minimumCacheTTLDefault 4 ghante (14400 seconds)
Local image + query string/logo.png?v=2 jaisi image ke liye images.localPatterns zaroori
priority propDeprecated, preload ya loading="eager" use karein
images.domainsDeprecated, remotePatterns use karein

6. Common mistakes

Galati: Remote image ka host config mein add na karna

Why? Bina remotePatterns ke Next.js remote URL optimize karne se mana kar deta hai aur error deta hai.

Behtar: next.config.ts mein images.remotePatterns ke andar exact protocol, hostname aur pathname likho.

Galati: fill use karte waqt sizes chhor dena

Why? Bina sizes ke browser samajhta hai image poori screen jitni chouri hai aur zaroorat se bari file download karta hai.

Behtar: Layout ke mutabiq sizes do, jaise mobile par 100vw aur desktop par 33vw.

Galati: Har image par preload lagana

Why? Sab images preload hongi to asal LCP image ko priority nahi milegi aur bandwidth zaya hogi.

Behtar: Sirf above the fold hero ya LCP image par preload rakho, baqi ko default lazy loading par chhor do.

Galati: Alt text khali ya bekar rakhna

Why? Screen reader users ko image ka matlab nahi milta aur image SEO bhi kamzor hota hai.

Behtar: Image kya dikha rahi hai woh mukhtasar likho, sirf decorative image ke liye alt khali rakho.

Interview tip

Sawal "CLS kam karne mein next/image kaise madad karta hai?" ka jawab: width aur height (ya static import) se browser ko aspect ratio pehle pata hota hai, is liye image load hone se pehle jagah reserve ho jati hai aur content nahi khiskta.

7. Pehle aur baad mein kya seekhein?

Final quick revision

PointDetail
Componentimport Image from "next/image"
Zaroori propssrc, alt, aur width / height ya fill
LCP imagepreload prop
Remote imagesimages.remotePatterns
Next 16 defaultsqualities: [75], minimumCacheTTL 4 ghante