-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JS-based lazy-loaded images should be replaced with native lazy-loading #1746
Comments
Hello, I am trying to work on this issue. Let me know for any suggestions and area of work, while I try to replicate/enhance the issue. Thanks ! |
@SohamPatel46 thanks! I did open a PR to start addressing part of this in #1785 but not to undo the lazy loading. The PR is more to fix handling of applying optimizations erroneously to JS lazy-loaded images. An issue I see at the moment is that I don't believe HTML Tag Processor is maybe suitable yet to do what is needed. Namely, if we need to remove a Then there is the question of to what degree we should go about undoing JS-based lazy loading. We should identify that the most common libraries are that do it. My impression is that LazySizes is the most popular and Gemini concurs. Nevertheless, I did find at least one other implementation in Avada when working on #1785. Lastly, there's the question if this should be in scope for Image Prioritizer in the first place. I recall @felixarntz has doubts, so we should make sure we're all on the same page. |
I just put together a prototype in #1872 (comment) for how this could be implemented while we wait for the HTML API to be able to remove tags. <?php
add_action( 'od_register_tag_visitors', function ( OD_Tag_Visitor_Registry $registry ) {
$registry->register( 'native-lazy-loading', function ( OD_Tag_Visitor_Context $context ) {
$processor = $context->processor;
if ( $processor->get_tag() !== 'IMG' || ! $processor->has_class( 'lazyload' ) ) {
return; // Tag is not relevant for this tag visitor.
}
$src = $processor->get_attribute( 'data-src' );
$srcset = $processor->get_attribute( 'data-srcset' );
if ( ! is_string( $src ) && ! is_string( $srcset ) ) {
return;
}
// Replace the src attribute with the data-src attribute.
if ( is_string( $src ) ) {
$processor->set_attribute( 'src', $src );
$processor->remove_attribute( 'data-src' );
}
// Replace the srcset attribute with the data-srcset attribute.
if ( is_string( $srcset ) ) {
$processor->set_attribute( 'srcset', $srcset );
$processor->remove_attribute( 'data-srcset' );
}
$processor->remove_class( 'lazyload' );
if ( $processor->next_tag() && $processor->get_tag() === 'NOSCRIPT' ) {
if ( method_exists( $processor, 'remove_tag' ) ) {
// Note: This is not yet supported by the HTML API! But this is how the NOSCRIPT could in theory be removed in the future.
$processor->remove_tag();
} elseif ( $processor->next_tag() && $processor->get_tag() === 'IMG' ) {
// This alternative prevents the duplicate NOSCRIPT > IMG from being shown.
$processor->remove_attribute( 'src' ); // Prevent loading the image.
$processor->remove_attribute( 'srcset' ); // Prevent loading the image.
$processor->set_attribute( 'hidden', true ); // Prevent the image from being rendered twice.
}
}
} );
} ); Naturally the Importantly, this tag visitor is expecting to be run before Image Prioritizer does its optimizations, to actually apply |
See #1745:
For example, Image Prioritizer should take the following from the Avada theme (code ref):
And transform it into:
And the following from Speed Optimizer from SiteGround (code ref):
Should be converted into:
Then Image Prioritizer's enhancements can apply as expected, with removing
loading=lazy
if theIMG
is in an initial viewport and also to add preloading as appropriate when it is the LCP element.There are many implementations of JS-based lazy-loading, however, so this should prioritize what is most popular (e.g. LazySizes). On the other hand, plugins which use JS-based lazy-loading could rather be engaged with to use native lazy-loading instead.
The text was updated successfully, but these errors were encountered: