Chapter 7. Tutorial: Optimize Your Site Step by Step

Step 4: Image Features

With your files optimized, turn on the features that change how images load. Enable them one at a time, checking your pages after each.

Add Image Attributes

When an image has no width and height in the page, the browser doesn't know how much space to keep for it, so the content below jumps down when it loads. Add Image Attributes adds the missing dimensions, which reduces CLS.

  1. Click Add Image Attributes in the Image/CDN Features panel of the dashboard.

  2. Check your pages, paying attention to image sizes, especially on phones.

Caution

If images now appear stretched, squashed or at the wrong size, your template displays them at a different size from their actual size without saying so in its CSS. Either resize the images to the size they are shown at, or switch this feature off and reserve the space with Custom CSS instead.

Lazy Load Images

Lazy Load Images delays loading images below the fold until the visitor scrolls towards them. In Pro, iframes (embedded maps and videos), audio, video and background images can be lazy loaded too.

  1. Click Lazy Load Images in the Image/CDN Features panel.

  2. Pro On the settings page, Media tab, Lazy Load Images section, switch on Lazy Load iframes, Audio/Video and Background Images.

  3. Check your pages, scrolling all the way down. Every image should appear as you reach it.

Figure 7.9. The Lazy Load Images settings
The Lazy Load Images section of the Media tab with its enable switch, media options and exclude and include lists.

Joomla

The Lazy Load Images section of the Media tab with its enable switch, media options and exclude and include lists.

WordPress


Fixing problems:

An image at the top of the page appears late, or not at all

Your fold is set too low for this page; go back to the section called “Set the Number of Elements Above the Fold”. For a single image, add it to Exclude these images.

Images in a slider or gallery don't appear

The slider's script moves images around in a way lazy loading can't follow. Exclude them by address, or in Pro by folder or by a class name they share (Exclude these classes/id).

Pro Lazy loading normally leaves everything above the fold alone. But some images at the top of a page aren't actually visible when it opens: the second and later slides of a slideshow, or icons in a drop-down menu. Loading them later speeds up the page. Add them with Include hidden images, by folder, or by a class name.

Pro Joomla Use YouTube facade replaces YouTube videos with a lightweight image of the video that loads the real player only when clicked.

Load AVIF and WebP Images Pro

If you created WebP or AVIF versions of your images in Step 2, Loads AVIF/WEBP Images serves them instead of the originals. When both exist, AVIF is used, because it is usually smaller. Images used as backgrounds in your CSS files are switched too.

  1. Click Load WEBP/AVIF in the Image/CDN Features panel.

  2. Check that your images still look right.

If one format causes problems, switch off just that one with Load WEBP images or Load AVIF images on the Media tab of the settings page, rather than the whole feature.

Load Responsive Images Pro

A phone doesn't need an image as wide as a desktop screen. If you created responsive images in Step 2, Load responsive images lets the browser pick the right size: images get srcset and sizes attributes listing the available sizes, and background images in your CSS switch size with the screen width. An image in a <picture> element that lists its own versions in <source> elements is left as it is: the page already chooses its size.

Phones get the smallest image, so images look softer on phones

On screens up to 576 pixels wide, JCH Optimize makes the browser use the smallest version (480 pixels wide by default), even on a phone with a sharp, high-resolution screen. A browser left to choose would usually pick a bigger file for those screens, two or three times as wide. This is on purpose: the smaller file loads much faster and helps your LCP score on phones. The cost is that photos can look a little less sharp on phones than they do without JCH Optimize.

If image quality on phones matters more to you than a faster LCP, don't switch Load responsive images off. On the settings page, Media tab, Load Responsive Images section, switch off Smallest image on phones instead. Phones then choose the version that suits their screen, as tablets and desktops always do: a high-resolution phone loads a larger file, so its images are sharp and take a little longer to load, and you keep the smaller files everywhere they are enough. Background images in your CSS follow the setting too.

  1. Click Load Responsive in the Image/CDN Features panel.

  2. Check your pages at phone size and desktop size. Images should look sharp on desktops and tablets. On phones they may look a little softer, as explained above. If that bothers you, switch off Smallest image on phones and check again.

LCP Images Pro

On most pages, the Largest Contentful Paint element is an image near the top: a hero banner, a featured image, the first slide. Telling the browser to fetch it first is one of the most effective ways to improve LCP. LCP Images adds a high-priority preload for the images you name, on any page where they appear, and makes sure they are never lazy loaded. An image in a <picture> element, or one your page already gives fetchpriority="high", isn't preloaded: it gets fetchpriority="high" instead, on phones and desktops alike, because a preload can't know which of a <picture>'s files the browser will choose.

Fix the page's layout shifts first. A shift can change which element is the largest when the page settles, so the image PageSpeed Insights names before the shifts are fixed may not be the one your visitors wait for.

With Page Experience on, you may not need to name them. On a page where none of your LCP images appear, JCH Optimize uses the image your visitors' browsers most often reported as the page's LCP element over the last two weeks, separately for phones and desktops. It waits until there are at least 10 reports for the page on that kind of device, and the same image in at least 6 out of 10 of them, so it doesn't guess on pages whose top changes, or whose LCP element is usually text. Pages without enough visitors are left alone. The image is only preloaded on pages where it still appears.

Procedure 7.5. Find and configure your LCP images
  1. Test a page with PageSpeed Insights. In the lab results, open the LCP breakdown or Largest Contentful Paint element insight; it shows the element. If it is an image (an <img>), right-click it on your page, choose Inspect, and copy the image's address from its src attribute.

    If it is another element, such as a <div>, the image is usually its background. Right-click the element on your page, choose Inspect, and look for background-image in the Styles pane. Copy the address inside url(…). A background image needs the preload even more than an <img>: the browser only finds it after it has downloaded and read the CSS, so without a preload it starts loading late.

    • If the background is set in the element's own style attribute, LCP Images finds it as it is.

    • If it comes from a CSS file, JCH Optimize finds it while it builds the critical CSS, so Optimize CSS Delivery must be on.

    • If the address is only in an attribute such as data-src, a script sets the background after the page loads, and LCP Images can't preload it. Exclude the element from the script's lazy loading, if the template or extension lets you, so that the background is in the page from the start.

    Figure 7.10. Finding the LCP element in PageSpeed Insights
    The LCP element insight in a PageSpeed Insights report, showing the image that is the largest contentful paint.

  2. On the settings page, Preloads tab, Largest Contentful Paint Images section, set Enable to Yes and add the image's address to LCP Images. An address that ends in the file's name, such as images/banners/hero.jpg, selects that image only, in whatever format it is served: the WebP or AVIF copy counts, but another size of it, such as hero-300x200.jpg, does not. If your page loads a size like that, add the size's own address. A part of an address without a file extension, such as banners/hero, selects every image whose address contains it, and the first one on the page gets the priority.

  3. If every page of one kind uses a different image in the same place (say, each article's featured image), add the class or id those images share to LCP Id/Class instead of adding every address.

  4. If phones and desktops show a different image at the top, use Mobile only and Desktop only.

  5. Save and test the page again. LCP should improve.

Figure 7.11. The LCP Images settings
The Largest Contentful Paint Images section of the Preloads tab with an image added.

Joomla

The Largest Contentful Paint Images section of the Preloads tab with an image added.

WordPress


Tip

One LCP image per page is enough. Preloading several makes them compete, and none arrives sooner.

Content Delivery Network (CDN)

If you use a CDN, this is the moment to switch it on in JCH Optimize so images, CSS and JavaScript load from it. Setting one up is a job of its own: see the section called “Use a Content Delivery Network (CDN)”.