Skip to content

Commit

Permalink
Perf: Increase number of eagerly loaded card images
Browse files Browse the repository at this point in the history
LCP image can end up being in the second row.

See #2679
  • Loading branch information
adamwoodnz committed Aug 13, 2024
1 parent 82f8a22 commit cfb2a66
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions wp-content/themes/pub/wporg-learn-2024/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* Actions and filters.
*/
add_action( 'after_setup_theme', __NAMESPACE__ . '\setup' );
add_filter( 'wp_get_attachment_image_attributes', __NAMESPACE__ . '\eager_load_first_card_row_images', 10, 3 );
add_filter( 'wp_get_attachment_image_attributes', __NAMESPACE__ . '\eager_load_first_card_rows_images', 10, 3 );
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\maybe_enqueue_sensei_assets', 100 );

Expand Down Expand Up @@ -139,20 +139,20 @@ function enqueue_assets() {
}

/**
* Eagerly load the images for the first row of cards, for performance (LCP metric).
* Eagerly load the images for the first 2 rows of cards, for performance (LCP metric).
*
* @param array $attr The image attributes.
* @param WP_Post $attachment The attachment post object.
* @param string $size The image size.
* @return array The modified image attributes.
*/
function eager_load_first_card_row_images( $attr, $attachment, $size ) {
function eager_load_first_card_rows_images( $attr, $attachment, $size ) {
static $image_count = 0;

if ( is_front_page() || is_archive() || is_search() || is_page( 'my-courses' ) ) {
$image_count++;

if ( $image_count <= 3 ) {
if ( $image_count <= 6 ) {
$attr['loading'] = 'eager';
$attr['fetchpriority'] = 'high';
}
Expand Down

0 comments on commit cfb2a66

Please sign in to comment.