Fixing a Slow LCP: When Does the Browser Find the Image?

Fixing a Slow LCP: When Does the Browser Find the Image?

LCP advice is everywhere, so we made a test page with one big hero image, 12 small slow images under it, and server delays which we control, and loaded it in Chrome changing only how the hero is included.

Plain img in the HTML gave LCP 400 ms. Same img with loading lazy, 1200 ms, three times worse for one word, because the browser waits with the request. As a CSS background, with the stylesheet taking 500 ms, it was 1216 ms, the picture can't be asked for until the CSS is there and parsed.

<img src="/img/hero.avif" fetchpriority="high"
     width="1200" height="600" alt="Warehouse at night">

Then we tried to save the background with preload in the head and got 612 ms, half of the pain is gone but it is still not as good as a normal img, since in our test the painting must wait for the stylesheet anyway.

<link rel="preload" as="image" href="/img/hero.avif" fetchpriority="high">

fetchpriority high we were sure will help, and it gave 404 ms against 400, so nothing. We think the reason is that our hero was already first in HTML and Chrome had no problem with the order, on a real page with more things competing the result can be other, but here we got zero and we say it.

These numbers are from localhost with fake delays, so only the proportions are interesting. When we look at a slow page now we ask first when the browser found out about the image, and only after how heavy it is.

import { onLCP } from 'web-vitals/attribution';

onLCP(({ value, attribution }) => {
  const body = JSON.stringify({
    value,
    target: attribution.target,
    ttfb: attribution.timeToFirstByte,
    loadDelay: attribution.resourceLoadDelay,
    loadDuration: attribution.resourceLoadDuration,
    renderDelay: attribution.elementRenderDelay,
  });
  navigator.sendBeacon('/vitals', body);
});

See also: A query plan and the image post and service worker and set a budget.