Skip to content

Commit 71b9259

Browse files
committed
Improve LCP by adding srcset for non-lazyloaded images
1 parent ae0d38f commit 71b9259

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public function test_retryable_error_and_error() {
231231
public function test_duplicated_image() {
232232

233233
$content = wp_get_attachment_image( self::$sample_attachement_upper_case );
234-
$this->assertEquals( "<img width=\"150\" height=\"150\" 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\" class=\"attachment-thumbnail size-thumbnail\" alt=\"\" decoding=\"async\" />", $content);
234+
$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);
235235
}
236236
public function test_page_images_process() {
237237
Optml_Tag_Replacer::$lazyload_skipped_images = 4;
@@ -258,7 +258,7 @@ public function test_page_images_process() {
258258
)
259259
);
260260
$replaced_content = Optml_Manager::instance()->replace_content( $content );
261-
$this->assertEquals( 3, substr_count($replaced_content, 'https://my_costum_domain'));
261+
$this->assertEquals( 7, substr_count($replaced_content, 'https://my_costum_domain'));
262262
$this->assertStringContainsString( '/w:150/h:150/q:75/rt:fill/g:ce/f:best/id:579c7f7707ce87caa65fdf50c238a117/http://example.org/sample-test.jpg', $replaced_content );
263263
$this->assertStringContainsString( '/w:150/h:150/q:eco/f:best/id:579c7f7707ce87caa65fdf50c238a117/http://example.org/sample-test.jpg', $replaced_content );
264264
}
@@ -333,7 +333,7 @@ public function test_custom_post_image_extraction () {
333333
public function test_special_characters_upload() : void {
334334
$special_character_attachment = self::factory()->attachment->create_upload_object( OPTML_PATH . 'tests/assets/special-characters-•⋿∀.jpg' );
335335
$content = wp_get_attachment_image( $special_character_attachment );
336-
$this->assertEquals( "<img width=\"150\" height=\"150\" 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\" class=\"attachment-thumbnail size-thumbnail\" alt=\"\" decoding=\"async\" />", $content );
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 );
337337
}
338338

339339
/**

0 commit comments

Comments
 (0)