Skip to content

Commit

Permalink
Fix lazyloading without srcset (#192)
Browse files Browse the repository at this point in the history
* fixed wrong attributes for image with lazy loading with empty srcset attribute

* added tests

* fixed jetpack js file with fix loading
  • Loading branch information
nk-o authored Oct 15, 2023
1 parent 991a124 commit a2ea795
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions assets/js/3rd/plugin-jetpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ $(document).on('loadedNewItems.vpf', function (event) {
});

// Fix masonry reloading when Jetpack images lazy loaded.
// https://github.com/Automattic/jetpack/issues/9595
//
// p.s. it looks like this fix is not working at all in Safari browser.
const runReLayout = debounce(200, ($gallery) => {
$gallery.vpf('imagesLoaded');
});
Expand Down
2 changes: 1 addition & 1 deletion classes/3rd/plugins/class-jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function wp_enqueue_scripts() {
return;
}

Visual_Portfolio_Assets::register_script( 'visual-portfolio-3rd-jetpack', 'assets/js/3rd/plugin-jetpack.min', array( 'jquery' ) );
Visual_Portfolio_Assets::register_script( 'visual-portfolio-3rd-jetpack', 'build/assets/js/3rd/plugin-jetpack', array( 'jquery' ) );

$wp_scripts->registered[ $jetpack_ll_handler ]->deps[] = 'visual-portfolio-3rd-jetpack';
}
Expand Down
2 changes: 1 addition & 1 deletion classes/class-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ public static function process_image_attributes( $attributes ) {

$attributes['data-src'] = $attributes['src'];

if ( isset( $attributes['srcset'] ) ) {
if ( ! empty( $attributes['srcset'] ) ) {
$attributes['data-srcset'] = $attributes['srcset'];

if ( $placeholder ) {
Expand Down
11 changes: 11 additions & 0 deletions tests/phpunit/unit/test-class-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ public function test_lazy_loading_srcset_sizes_attributes() {
$image_string = '<img loading="lazy" src="image.jpg" srcset="image.jpg 2x" sizes="100vw" alt="Test Image" width="10" height="10">';
$lazy_string = '<img loading="eager" src="image.jpg" srcset="' . $placeholder . '" alt="Test Image" width="10" height="10" data-src="image.jpg" data-srcset="image.jpg 2x" data-sizes="auto" class="vp-lazyload">';

$this->assertEquals(
$this->get_noscript_image( $image_string ) . $lazy_string,
Visual_Portfolio_Images::add_image_placeholders(
$image_string
)
);

// When srcset exists, but it is empty.
$image_string = '<img loading="lazy" src="image.jpg" srcset alt="Test Image" width="10" height="10">';
$lazy_string = '<img loading="eager" src="' . $placeholder . '" srcset alt="Test Image" width="10" height="10" data-src="image.jpg" data-sizes="auto" class="vp-lazyload">';

$this->assertEquals(
$this->get_noscript_image( $image_string ) . $lazy_string,
Visual_Portfolio_Images::add_image_placeholders(
Expand Down

0 comments on commit a2ea795

Please sign in to comment.