Make WordPress Core


Ignore:
Timestamp:
05/22/2023 07:11:36 PM (3 years ago)
Author:
flixos90
Message:

Media: Conditionally skip lazy-loading on images before the loop to improve LCP performance.

When the logic to exclude images that likely appear above the fold from being lazy-loaded was introduced in WordPress 5.9, initially only images that appear within the main query loop were being considered. However, there is a good chance that images above the fold are rendered before the loop starts, for example in the header template part.

It is particularly common for a theme to display the featured image for a single post in the header. Based on HTTP Archive data from February 2023, the majority of LCP images that are still being lazy-loaded on WordPress sites use the wp-post-image class, i.e. are featured images.

This changeset enhances the logic in wp_get_loading_attr_default() to not lazy-load images that appear within or after the header template part and before the query loop, using a new WP_Query::$before_loop property.

For block themes, this was for the most part already addressed in [55318], however this enhancement implements the solution in a more generally applicable way that brings the improvement to classic themes as well.

Props thekt12, flixos90, spacedmonkey, costdev, zunaid321, mukesh27.
Fixes #58211.
See #53675, #56930.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r55822 r55847  
    108108         */
    109109        public $current_post = -1;
     110
     111        /**
     112         * Whether the caller is before the loop.
     113         *
     114         * @since 6.3.0
     115         * @var bool
     116         */
     117        public $before_loop = true;
    110118
    111119        /**
    … …  
    518526                $this->current_post = -1;
    519527                $this->in_the_loop  = false;
     528                $this->before_loop  = true;
    520529                unset( $this->request );
    521530                unset( $this->post );
    … …  
    36323641
    36333642                $this->in_the_loop = true;
     3643                $this->before_loop = false;
    36343644
    36353645                if ( -1 == $this->current_post ) { // Loop has just started.
    … …  
    36723682                        $this->rewind_posts();
    36733683                } elseif ( 0 === $this->post_count ) {
     3684                        $this->before_loop = false;
     3685
    36743686                        /**
    36753687                         * Fires if no results are found in a post query.
Note: See TracChangeset for help on using the changeset viewer.