Largest Contentful Paint (LCP) Optimization Fix
In this tutorial, you'll learn about Largest Contentful Paint (LCP) Optimization Fix. We cover key concepts, practical examples, and best practices.
Your Lighthouse report shows a poor Largest Contentful Paint (LCP) score above 2.5 seconds — the largest visible element (hero image, heading, or video) takes too long to render due to slow server response or unoptimized assets.
Step-by-Step Fix
1. Identify the LCP element
Run Lighthouse and check the "LCP element" in the Diagnostics section.
Common LCP elements:
- Hero image
- Large heading text
- Full-width video
2. Optimize the LCP image
<!-- Wrong — LCP image loaded lazily or without preload -->
<img src="hero-4000.jpg" alt="Hero" loading="lazy">
<!-- Right — preload the LCP image and serve optimized size -->
<link rel="preload" href="hero-1200.webp" as="image">
<img src="hero-1200.webp"
srcset="hero-800.webp 800w,
hero-1200.webp 1200w,
hero-2000.webp 2000w"
sizes="(max-width: 800px) 100vw,
(max-width: 1200px) 1200px,
2000px"
alt="Hero"
fetchpriority="high">
3. Improve server response time (TTFB)
# Enable compression
gzip on;
gzip_types text/html text/css application/javascript image/svg+xml;
# Enable caching for static assets
location /static/ {
expires 1y;
add_header Cache-Control "public, immutable";
}
4. Use a CDN
# Move static assets to a CDN
# Wrong — serving everything from origin server
# https://myapp.com/static/hero.webp
# Right — serve from CDN
# https://cdn.myapp.com/static/hero.webp
Replace the asset URLs in the application:
// Wrong — serving from origin
const HERO_IMAGE = "/static/hero.webp";
// Right — serve from CDN
const HERO_IMAGE = "https://cdn.myapp.com/static/hero.webp";
Common Mistakes
| Mistake | Fix |
|---|---|
| Lazy loading the LCP element | Set fetchpriority="high" and remove loading="lazy" from LCP image |
| Serving full-resolution hero image | Resize the hero to the maximum display size (usually 1200-2000px) |
| Slow server response time (TTFB >800ms) | Use CDN, optimize backend, enable caching |
| Render-blocking CSS/JS blocking LCP | Inline critical CSS and defer non-critical scripts |
| No preload on hero image | Add <link rel="preload"> for the hero image |
Prevention
- Identify the LCP element during development and prioritize it.
- Preload the LCP image with
fetchpriority="high". - Keep server TTFB under 800ms with CDN and caching.
- Resize hero images to no more than 2000px wide.
- Use WebP/AVIF format for hero images.
DodaTech Tools
Doda Browser's performance tab identifies the LCP element for any page and shows how to optimize it. DodaZIP compresses hero images without quality loss for faster LCP. Durga Antivirus Pro preloads its critical UI assets to achieve sub-second LCP on modern hardware.
Common Mistakes with contentful paint
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world LARGEST code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro