← Back to React Course | Chapter 14: Performance & Production Deployment | Lesson 6 of 13

Image Optimization की Techniques

Image optimization एक suitcase को efficiently pack करने जैसा है — आप चाहते हैं photo अब भी अच्छी दिखे, सिर्फ इसकी ज़रूरत से कहीं ज़्यादा space लिए बिना।
Syntax
markup
<img
  src="image.jpg"
  srcSet="small.jpg 400w, large.jpg 800w"
  sizes="(max-width: 600px) 400px, 800px"
  width="800"
  height="600"
  loading="lazy"
  alt="Description"
/>

Appropriately-Sized Images Serve करना

एक huge, full-resolution image load करना और इसे CSS के through छोटा display करना फिर भी FULL file size download करता है — browser download को magically shrink नहीं करता।

एक image को इसकी actual display dimensions के close पहले से sized serve करना उन pixels पर bandwidth waste करने से बचता है जो कभी actually दिखाए नहीं जाते।

Note: Responsive designs के लिए, <picture> element या srcset attribute अलग screen widths के लिए automatically अलग image sizes serve कर सकते हैं।
Warning: एक 4000px-wide photo जो एक 200px-wide thumbnail में displayed हो फिर भी user को पूरे 4000px download की cost लगती है — CSS resizing actual file transfer को कम नहीं करता।

उदाहरण: Serving Appropriately-Sized Images

markup
<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
  <div id="root"></div>
  <script type="text/babel">
function ProductCard() {
  return <img src="product-thumb-400w.jpg" width="200" height="200" alt="Product" />;
}
ReactDOM.createRoot(document.getElementById('root')).render(<ProductCard />);
  </script>
</body>
</html>

Off-Screen Images Lazy Load करना

Native 'loading="lazy"' attribute browser को बताता है कि एक image download तब तक defer करे जब तक यह view में scroll होने वाली न हो, सब कुछ upfront load करने के बजाय।

यह initial visible viewport के नीचे वाली images के लिए किसी extra library की ज़रूरत न होने वाला एक simple, built-in optimization है।

Note: उन images को lazy-load न करें जो page load पर immediately visible हैं (जैसे एक hero banner) — वह actually उन्हें दिखाने में delay करेगा।
Warning: उन images को lazy-loading करना जो IMMEDIATELY visible हैं इनके appear होने से पहले एक noticeable delay cause कर सकता है, क्योंकि browser download शुरू करने से पहले wait करता है।

उदाहरण: Lazy Loading Off-Screen Images

markup
<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
  <div id="root"></div>
  <script type="text/babel">
function Gallery() {
  const images = ["photo1.jpg", "photo2.jpg", "photo3.jpg"];
  return (
    <div>
      {images.map((src, i) => (
        <img key={src} src={src} loading={i === 0 ? "eager" : "lazy"} alt={"Photo " + (i + 1)} width="150" />
      ))}
    </div>
  );
}
ReactDOM.createRoot(document.getElementById('root')).render(<Gallery />);
  </script>
</body>
</html>

Layout Shift रोकना

बिना explicit width/height के, layout में एक image की space तब तक unknown है जब तक यह load पूरा न हो, जिससे surrounding content हर image pop in होने पर visibly jump करता है।

Width और height (या एक matching aspect-ratio) set करना upfront correct space reserve करता है, layout को stable रखते हुए।

Note: Even अगर actual displayed size CSS से controlled हो, width/height attributes set करना फिर भी browser को correct aspect ratio early reserve करने में मदद करता है।
Warning: Images पर missing width/height एक poor Cumulative Layout Shift (CLS) score के सबसे common causes में से एक है, एक real, measured web performance metric।

उदाहरण: Preventing Layout Shift

markup
<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
  <div id="root"></div>
  <script type="text/babel">
function Article() {
  return (
    <div>
      <p>Some text before the image.</p>
      <img src="diagram.jpg" width="400" height="300" alt="Diagram" style={{ display: "block" }} />
      <p>Text after — doesn't jump around once width/height are set.</p>
    </div>
  );
}
ReactDOM.createRoot(document.getElementById('root')).render(<Article />);
  </script>
</body>
</html>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक full-resolution image serve करना (जैसे एक 4000px-wide photo) जब यह सिर्फ 400px wide display होती है।
  2. Page में बहुत नीचे वाली images के लिए lazy loading उपयोग न करना, browser को उन्हें download करने के लिए मजबूर करते हुए इससे पहले कि वे कभी दिखें।
  3. Images पर missing width/height attributes, हर image load होते और content push करते हुए layout shift का कारण बनते।
चैप्टर सारांश
  • Appropriately-sized images serve करना (उनकी actual display size से match करते हुए) wasted bandwidth से बचता है।
  • Native loading=lazy attribute user के पास scroll होने तक off-screen images loading defer करता है।
  • Explicit width/height (या aspect-ratio) set करना images load होने पर layout shift रोकता है।
  • WebP/AVIF जैसे modern formats JPEG/PNG जैसे older formats से same visual quality के लिए बेहतर compression offer करते हैं।
ब्राउज़र सपोर्ट

loading=lazy सभी modern browsers में supported है; WebP/AVIF support broad है लेकिन ज़रूरत हो तो बहुत पुराने browsers के लिए हमेशा एक fallback दें।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.