Skip to Content

Preloading LCP images

About

It’s important to preload LCP images to improve your site performance. Breeze already does this efficiently for generic CMS, Category, and Product pages using the following approach:

CMS page

Product page

Category page

While this approach if great and doesn’t require coding from theme developers to make it work, sometimes you don’t have access to the markup, or the page is complex and you want to define your own rules.

Source Code:

Preload using CSS class

Product page always preload main gallery image only.

This method is very simple. Just add data-preload class name and Breeze will preload the image:

<img src="..." class="data-preload"/>

<!-- OR -->

<figure class="data-preload">
    <img src="..."/>
</figure>

Warning!
When using this approach, Breeze automatically disables it’s automatic detection.

Preload using XML layout update

Product page always preload main gallery image only.

With this powerful method you can preload images of any content generated by third-party module. You can also use it to create different rules per different layout update handles.

Let’s create a custom rule for category page that will preload first two third-party images when a third-party widget is added above product listing:

Create catalog_category_view.xml layout update in your theme:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="breeze.criticalImages">
            <arguments>
                <argument name="image_xpaths" xsi:type="array">
                    <item name="unique_name" xsi:type="string"><![CDATA[//div[contains(@class, "third-party")]//li[@class="item" and position()<=2]//img]]></item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

That’s all. Now, when Breeze will find the images inside <div class="third-party"> it will add fetchpriority="high" attribute to each matched element and add preload links to the document head section.

If Breeze will not find the images, then it will use built-in logic for this page.