All articles

Adding fetchpriority to a Drupal responsive image hero for better LCP

If the Largest Contentful Paint element on a Drupal page is a hero photo rendered by the core responsive image formatter, the formatter lets you set loading="eager" but offers no option for fetchpriority="high". You can add it from a theme's hook_preprocess_responsive_image_formatter(), scoped to the one responsive image style the hero uses. The rest of the work is making sure the browser picks the right file: fill gaps in the srcset, write sizes that match the real layout, and preload any decorative images that sit above the fold.

This is a walk-through of how a teammate tuned the homepage hero on a group of nonprofit membership sites that share one Drupal distribution. I didn't write the change, but it is a tidy example of doing Core Web Vitals work inside Drupal's rendering pipeline rather than around it.

Why eager loading is not enough for the LCP image

loading="eager" only tells the browser not to defer the image. It still competes with stylesheets, scripts, fonts and every other image discovered early, and browsers tend to start images at a modest priority until layout shows they are in the viewport. For the image that defines LCP, that delay lands directly on the metric.

fetchpriority="high" is the hint for exactly this case: it tells the browser this one image matters more than the others. It belongs on very few images per page. Put it on every image and it means nothing.

How to add fetchpriority to one responsive image style

The core formatter's preprocess function builds a render array for the responsive image and passes it on in $variables['responsive_image'], along with the style id in $variables['responsive_image_style_id']. Theme preprocess hooks run after the core one, so a theme can add an attribute to the render array before it reaches the template. Attributes on that array end up on the <img> inside the <picture> element.

<?php

/**
 * Implements hook_preprocess_HOOK() for responsive_image_formatter.
 */
function mytheme_preprocess_responsive_image_formatter(array &$variables): void {
  if (($variables['responsive_image_style_id'] ?? NULL) === 'homepage_hero') {
    $variables['responsive_image']['#attributes']['fetchpriority'] = 'high';
  }
}

Keying on the responsive image style, rather than on the field, the bundle or the route, is the decision I like most here. The style is already the thing that says "this is the hero". Any page that renders an image through that style gets the hint, and no other image does. If an editor later uses the same media field in a card, the card uses a different style and stays at normal priority.

Pair it with the formatter's own Image loading setting set to Eager for that display. A lazy image with high fetch priority is a contradiction the browser resolves in favour of lazy.

Why a gap in the srcset sends the wrong file

A responsive image style maps breakpoints to image styles, and Drupal turns those into srcset candidates with width descriptors. The browser picks the smallest candidate at least as wide as the slot multiplied by the device pixel ratio. If the candidates jump from, say, 750 to 1500 pixels, any slot that needs 800 pixels downloads the 1500-pixel file. On a hero that is the largest image on the page, that is a lot of wasted bytes on the critical path.

The team's fix was to add an intermediate image style to the responsive image style, so there is a candidate between the two widths that were too far apart. It costs one more derivative per image on the server and nothing on the client.

Why sizes="100vw" is usually wrong

sizes tells the browser how wide the image will be before layout has run. 100vw is the safe default, and it is often far too large: a card in a two-column grid on a tablet is nowhere near the full viewport width. With 100vw the browser assumes the worst and downloads a bigger candidate than the slot needs.

The team tightened the sizes values on the card images to match the real layout widths on mobile. That does not touch the LCP element directly, but every image that downloads less leaves more bandwidth for the one that does matter.

<img
  srcset="card-400.webp 400w, card-640.webp 640w, card-960.webp 960w"
  sizes="(min-width: 64em) 30vw, (min-width: 40em) 45vw, calc(100vw - 2rem)"
  src="card-640.webp" alt="" loading="lazy">

Preloading decorative hero graphics

The hero also had decorative graphics around the photo. Images that the preload scanner meets late in the markup, or only once a component renders, start downloading late. The team gave those graphics their own smaller WebP variants with srcset and sizes, and preloaded them from the theme's html.html.twig so the browser starts fetching them from the document head. A responsive preload uses imagesrcset and imagesizes so the browser still picks the right variant:

<link rel="preload" as="image"
  imagesrcset="/themes/custom/mytheme/images/shape-480.webp 480w,
               /themes/custom/mytheme/images/shape-960.webp 960w"
  imagesizes="(min-width: 64em) 40vw, 80vw">

Preload only what is actually above the fold on the pages that load the template. A preload for an image that a page never shows is a wasted request at the highest priority, and browsers warn about it in the console.

How to check the result

No before-and-after figures came with this change, so I won't quote any. To measure your own, look at the LCP element and its resource timing in the Chrome DevTools Performance panel: you want the hero request to start early and not queue behind other images. Lighthouse and PageSpeed Insights both flag an LCP image that lacks fetchpriority="high" or is lazy-loaded, which makes a quick regression check.

Key takeaways

  • Drupal's responsive image formatter has no fetchpriority option, but a theme's hook_preprocess_responsive_image_formatter() can add the attribute to the render array.
  • Scope fetchpriority="high" to the responsive image style the hero uses, so exactly one image per page gets it.
  • A large gap between srcset widths makes the browser download a much bigger file than the slot needs; an intermediate image style closes it.
  • sizes="100vw" overstates most non-hero images, so match sizes to the real layout widths.
  • Above-the-fold decorative images can be fetched from the document head with a responsive preload, using imagesrcset and imagesizes so the browser still picks the right variant.

Written by Artem Shymko, a senior software engineer in Belgium working on Drupal, .NET and LLM features. Get in touch if you are working on something similar.

  • Drupal
  • Core Web Vitals
  • Responsive images
  • Performance