Skip to content

Commit 95086f4

Browse files
authored
Merge pull request #798 from Codeinwp/improve_lcp
Improve LCP by adding srcset for non-lazyloaded images
2 parents 1f9168e + e8e3cae commit 95086f4

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

inc/lazyload_replacer.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,6 @@ public function init() {
196196
return;
197197
}
198198

199-
add_filter(
200-
'max_srcset_image_width',
201-
function () {
202-
return 1;
203-
}
204-
);
205199
self::$is_lazyload_placeholder = self::$instance->settings->get( 'lazyload_placeholder' ) === 'enabled';
206200

207201
add_filter( 'optml_tag_replace', [ $this, 'lazyload_tag_replace' ], 2, 6 );
@@ -322,6 +316,12 @@ public function lazyload_tag_replace( $new_tag, $original_url, $new_url, $optml_
322316
$new_tag,
323317
1
324318
);
319+
320+
// We remove srcset and sizes attributes since our lazyload does not need them.
321+
$pattern = '/\s+(?:srcset|sizes)=("[^"]*"|\'[^\']*\'|\\\\"[^\\\\"]*\\\\")/i';
322+
$new_tag = preg_replace( $pattern, '', $new_tag );
323+
324+
// We keep this for srcset lazyload compatibility that might break our mechanism.
325325
$new_tag = str_replace( 'srcset=', 'old-srcset=', $new_tag );
326326

327327
if ( ! $this->should_add_noscript( $new_tag ) ) {

tests/test-lazyload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function test_lazyload_tag_sanity_check() {
112112
$this->assertStringContainsString( '</noscript></a>', $replaced_content, 'Noscript tag should be inside the wrapper tag and after image tag' );
113113
$this->assertStringNotContainsString( '"http://example.org', $replaced_content );
114114
$this->assertEquals( 1, substr_count( $replaced_content, 'q:eco' ) );
115-
$this->assertEquals( 2, substr_count( $replaced_content, 'old-srcset' ) );
115+
$this->assertEquals( 1, substr_count( $replaced_content, 'old-srcset' ) );
116116

117117
}
118118
public function test_lazyload_skip_standard_class() {

tests/test-media.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ public function test_retryable_error_and_error() {
229229
public function test_duplicated_image() {
230230

231231
$content = wp_get_attachment_image( self::$sample_attachement_upper_case );
232-
$this->assertEquals( "<img width=\"150\" height=\"150\" src=\"https://example.i.optimole.com/w:150/h:150/rt:fill/g:ce/q:mauto/process:". self::$sample_attachement_upper_case ."/id:579c7f7707ce87caa65fdf50c238a117/http://example.org/1PQ7p-2.jpg\" class=\"attachment-thumbnail size-thumbnail\" alt=\"\" decoding=\"async\" />", $content);
232+
$this->assertStringContainsString( "src=\"https://example.i.optimole.com/w:150/h:150/q:mauto/rt:fill/g:ce/process:". self::$sample_attachement_upper_case ."/id:579c7f7707ce87caa65fdf50c238a117/http://example.org/1PQ7p-2.jpg\" ", $content);
233+
233234
}
234235
public function test_page_images_process() {
235236
Optml_Tag_Replacer::$lazyload_skipped_images = 4;
@@ -256,7 +257,7 @@ public function test_page_images_process() {
256257
)
257258
);
258259
$replaced_content = Optml_Manager::instance()->replace_content( $content );
259-
$this->assertEquals( 3, substr_count($replaced_content, 'https://my_costum_domain'));
260+
$this->assertEquals( 7, substr_count($replaced_content, 'https://my_costum_domain'));
260261
$this->assertStringContainsString( '/w:150/h:150/q:75/rt:fill/g:ce/f:best/id:579c7f7707ce87caa65fdf50c238a117/http://example.org/sample-test.jpg', $replaced_content );
261262
$this->assertStringContainsString( '/w:150/h:150/q:eco/f:best/id:579c7f7707ce87caa65fdf50c238a117/http://example.org/sample-test.jpg', $replaced_content );
262263
}
@@ -331,7 +332,9 @@ public function test_custom_post_image_extraction () {
331332
public function test_special_characters_upload() : void {
332333
$special_character_attachment = self::factory()->attachment->create_upload_object( OPTML_PATH . 'tests/assets/special-characters-•⋿∀.jpg' );
333334
$content = wp_get_attachment_image( $special_character_attachment );
334-
$this->assertEquals( "<img width=\"150\" height=\"150\" src=\"https://example.i.optimole.com/w:150/h:150/rt:fill/g:ce/q:mauto/process:". $special_character_attachment ."/id:579c7f7707ce87caa65fdf50c238a117/http://example.org/special-characters-•⋿∀.jpg\" class=\"attachment-thumbnail size-thumbnail\" alt=\"\" decoding=\"async\" />", $content );
335+
336+
$this->assertStringContainsString( "src=\"https://example.i.optimole.com/w:150/h:150/q:mauto/rt:fill/g:ce/process:". $special_character_attachment ."/id:579c7f7707ce87caa65fdf50c238a117/http://example.org/special-characters-•⋿∀.jpg\" ", $content );
337+
335338
}
336339

337340
/**

0 commit comments

Comments
 (0)