Skip to content

Commit 5a87fda

Browse files
authored
Merge pull request #786 from Codeinwp/improve/lcp
add fetch priority for lazyload bypassed images to improve lcp
2 parents d8e1047 + 97b2e07 commit 5a87fda

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

inc/tag_replacer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,12 @@ public function regular_tag_replace( $new_tag, $original_url, $new_url, $optml_a
331331
$new_tag = $is_slashed ? str_replace( 'loading=\"lazy\"', 'loading=\"eager\"', $new_tag ) : str_replace( 'loading="lazy"', 'loading="eager"', $new_tag );
332332
}
333333
}
334+
// If the image is between the first images we add the fetchpriority attribute to improve the LCP.
335+
if ( self::$lazyload_skipped_images < Optml_Lazyload_Replacer::get_skip_lazyload_limit() ) {
336+
if ( strpos( $new_tag, 'fetchpriority=' ) === false ) {
337+
$new_tag = preg_replace( '/<img/im', $is_slashed ? '<img fetchpriority=\"high\"' : '<img fetchpriority="high"', $new_tag );
338+
}
339+
}
334340

335341
self::$lazyload_skipped_images ++;
336342
return preg_replace( $pattern, $replace, $new_tag );

0 commit comments

Comments
 (0)