-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Auto Sizes for Lazy-loaded Images #7253
Auto Sizes for Lazy-loaded Images #7253
Conversation
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mukeshpanchal27 This looks great to me! Only a few minor points of feedback.
Co-authored-by: Felix Arntz <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mukeshpanchal27 Awesome stuff, looks great!
A few minor nit-pick comments, but no blockers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it's working as expected. A couple thoughts...
Most dynamic optimizations WP applies can be short circuited through a filter, but there doesn't seem to be an easy way to disable this functionality. I think both places where we are adding auto sizes, we should add a filter like:
$is_auto_sizes_enabled = apply_filters( 'wp_img_auto_sizes_enabled`, true );
I also think this logic could all be simplified a bit if we added auto-sizes in wp_img_tag_add_loading_optimization_attrs()
rather than a separate function. In the Enhanced Responsive Images plugin, creating a separate function was required since there wasn't a way to filter the image attributes during wp_filter_content_tags()
when the functionality was first written.
@joemcgill Being able to disable auto sizes could be a good idea. Though I would lean against baking this functionality into |
Co-authored-by: Felix Arntz <[email protected]>
@joemcgill Thanks for the feedback. In deec3ba, I introduced a new filter that disables the auto sizes functionality. I also agree with what @felixarntz mentioned #7253 (comment). |
@joemcgill @mukeshpanchal27 Given this some further thought, I'll have to revise my take on having a filter. I'm questioning why we should introduce one to allow disabling auto sizes, for a few reasons:
@joemcgill Can you provide a rationale on how a filter to disable auto sizes would help? |
The |
Which lazy-loading filter are you referring to? As far as I'm aware, it's not possible for the logic here to add |
In quick test i add
You are right here. |
I don't understand. I think that filter only runs for content images, not |
Sure. This functionality introduces an optimization that should work well for a majority of sites—particularly ones that aren't using any plugins or custom code that modifies the way WordPress handles lazy-loading. However, for sites which have a unique setup where this functionality could negatively impacts their site's performance, I think it's good practice to allow sites to easily be able to opt out of these optimizations through the use of a filter, so they can customize how images are served to better fit their specific use case. In the specific case of auto sizes, if |
To me, your points are only theoretical concerns. The logic here only adds Even if let's say a plugin removes WordPress's lazy-loading attributes from an image, the code here to add |
@felixarntz We've already seen cases in plugins that the Performance Team has written, like Image Prioritizer (related issue), where the Given that sites can still work around any issues with this functionality by using filters that run after this is applied, I'm ok with us not adding a filter to disable this functionality initially, if you feel strongly about it. That said, I do generally think it's good practice for us to give easy ways to opt-out of these types of optimization decisions that affect the markup of a site since we can't account for every use case a site might have and this type of filter is not all that difficult to maintain. |
src/wp-includes/media.php
Outdated
/** | ||
* Filters whether the auto sizes enabled for lazy load images. | ||
* | ||
* @since 6.7.0 | ||
* | ||
* @param bool $is_auto_sizes_enabled Whether the auto sizes enabled for lazy load images. Default true. | ||
*/ | ||
$is_auto_sizes_enabled = apply_filters( 'wp_img_auto_sizes_enabled', true ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we add a filter to opt out of this feature (pending this discussion), I think a better location would be inside the wp_img_tag_add_auto_sizes()
function, and have that function return the original sizes
value if the filter is set to false
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per #7253 (comment), I'd still prefer not to have one, but this would work for me, if y'all feel strongly about having one. Basically the filter should work like wp_img_tag_add_loading_attr
, i.e. also receive the image and context.
That's fair, though the way that this is achieved by that plugin is a (valid) hack via output buffering, so IMO not something core should cater for. I think Core should ensure that anything that is possible via its own APIs (actions and filters) works as expected. You can modify basically anything with things like output buffer, modifying globals, etc., but those aren't encouraged to use, or rather "use at your own risk". So from that perspective I don't find the Image Prioritizer usage relevant for this.
Related to your comment in #7253 (review), I would say that if we introduce a filter, it should be specific to each image (e.g. receive the image tag and context like e.g. the My primary concern is about having a filter to generally enable/disable |
Totally agree that it would be useful for someone to filter whether this is enabled on based on which specific image the filter was being applied to. However, I think we should go ahead and commit this without a filter for now. We can determine whether a filter is needed and what context will need to be added to that filter after this functionality has been in trunk for a while. |
I agree. In e4a141a, I have removed the filter. The PR is now ready for review and commit. 🎉 Should we open a separate Trac ticket for this discussion now, or can we wait and open one in the future if any issues arise? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mukeshpanchal27 Looks great! 🎉
Regarding a potential filter, I think we can keep discussing on the Trac ticket if needed.
Co-authored-by: Weston Ruter <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One other nit, but pre-approving.
isset( $attr['sizes'] ) && | ||
! wp_sizes_attribute_includes_valid_auto( $attr['sizes'] ) | ||
) { | ||
$attr['sizes'] = 'auto, ' . $attr['sizes']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this remove auto
if it is present in $attr['sizes']
while $attr['loading']
is not lazy
? This is implemented in WordPress/performance#1476.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this remove
auto
if it is present in$attr['sizes']
while$attr['loading']
is notlazy
? This is implemented in WordPress/performance#1476.
Do any of the implementations respect it in spite of the spec if the CSS is available and the image isn't set for lazy loading? If so then I think it would be good to keep it given the sizes attributes can be inaccurate in many situations and the CSS may be in the HTML header and therefore available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this remove
auto
if it is present in$attr['sizes']
while$attr['loading']
is notlazy
? This is implemented in WordPress/performance#1476.
@westonruter Core only adds the "auto" in sizes when image is lazy-loaded so if user added that through filter then core will not remove "auto".
@peterwilsoncc Even if the CSS is available early in the document (such as in the HTML header), implementations do not dynamically adjust the sizes attribute based on CSS unless lazy loading is enabled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is needed as Core doesn't add sizes="auto"
in a way where this could be relevant. If a plugin does that, it's doing it wrong.
Now I'm not strongly against adding this, but it's not a requirement for this feature. We could discuss whether this is worth adding in a separate ticket, or wait if it comes up as a real use-case problem somewhere.
Co-authored-by: Weston Ruter <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good initial pass at adding this functionality. Let's commit this and iterate on questions like this one with the benefit of more testing.
Trac ticket: https://core.trac.wordpress.org/ticket/61847
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.