responsive.sk

Article

Core Web Vitals in Practice: LCP, srcset, Lazy Loading

Article cover illustration: Architecture, seo, performance, core-web-vitals

Core Web Vitals in Practice: LCP, srcset, Lazy Loading

Core Web Vitals are three metrics Google uses to evaluate user experience. In Nativa Minimal we optimized each one.

LCP (Largest Contentful Paint)

LCP measures how fast the main content loads, typically the cover image on a blog article.

Problem

Imagine a cover image at 720x360px. On mobile, 360px width is enough. We're loading 2x more pixels than needed.

Solution: srcset

private function coverSrcset(string $image): string
{
    if (!str_contains($image, 'res.cloudinary.com')) {
        return '';
    }

    $small = str_replace('w_720,h_360', 'w_360,h_180', $image);
    $medium = str_replace('w_720,h_360', 'w_634,h_317', $image);

    return ' srcset="' . $small . ' 360w, '
        . $medium . ' 634w, '
        . $image . ' 720w"'
        . ' sizes="(min-width: 1080px) 330px, '
        . '(min-width: 640px) 520px, 100vw"';
}

The browser picks the optimal size:

  • Desktop (1080px+): 360w (330px card)
  • Tablet (640-1080px): 634w (520px card)
  • Mobile: 360w (100vw)

Eager vs Lazy

First 3 images (first row) are eager + fetchpriority="high": LCP candidates:

$loading = $inFirstRow ? 'eager' : 'lazy';
$priority = $inFirstRow ? ' fetchpriority="high"' : '';

The rest are lazy: loaded after scroll.

CLS (Cumulative Layout Shift)

CLS measures visual stability, how much content shifts during loading.

Problem

An image without width/height causes layout shift, text moves when the image loads.

Solution: explicit dimensions

$cover = '<img class="st-blog-index__card-image" src="..."'
    . ' width="720" height="360"'
    . ' loading="' . $loading . '"' . $priority
    . ' decoding="async">';

The browser knows in advance how much space the image takes, no shift.

FID/INP (First Input Delay / Interaction to Next Paint)

FID measures how fast the page responds to the first interaction. INP is its successor.

Solution: minimal JavaScript

Nativa Minimal uses:

  • HTMX: dynamic content without a framework
  • Inline scripts: only for CSS switcher and select-all
  • No external CDNs: everything self-hosted
<script nonce="d80d34d15b3cd313f185796de33310b7">
(function(){
    var sa = document.getElementById('select-all');
    if (sa) sa.addEventListener('change', function(){
        document.querySelectorAll('.checkbox').forEach(function(c){
            c.checked = sa.checked;
        });
    });
})();
</script>

Entire admin panel: ~15KB JavaScript. No React, no Vue, no Angular.

Tests

Core Web Vitals are covered by tests:

public function testFirstCardImageIsEagerLcp(): void
{
    $html = (new BlogIndexAction())->handle(...)->getContent();

    self::assertStringContainsString(
        'loading="eager" fetchpriority="high"',
        $html
    );
    self::assertSame(3, substr_count($html, 'loading="eager"'));
}

The solution was three attributes: loading="eager", fetchpriority="high", and srcset. One test per pattern. The code you read tomorrow should not make you guess why the image loads fast, the test explains it.

Core Web Vitals remains the simplest performance contract, just not one you should skip. When you know what LCP, CLS, and INP measure, the fixes become obvious. The next person who deploys your app should not have to guess which image loads first.

→ Article cover LCP: the first image that matters → LCP srcset basics: responsive images done right → Reading Lighthouse report: detective work on audits

Sources

Comments (0)

No comments yet — be the first to react.

Reader questions (0)

No questions yet — be the first to ask.

Stuck on something in this article? Ask below. Your name is shown publicly; your email is never stored. A question appears here once a moderator approves it.