-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Fix first image lazy loading with block themes #3538
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
Fix first image lazy loading with block themes #3538
Conversation
Disable wp_lazy_loading_enabled by default when context is false
| // See https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-loading | ||
| // See https://html.spec.whatwg.org/multipage/iframe-embed-object.html#attr-iframe-loading | ||
| $default = ( 'img' === $tag_name || 'iframe' === $tag_name ); | ||
| $default = false !== $context && ( 'img' === $tag_name || 'iframe' === $tag_name ); |
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.
There are several places we could adjust for block theme behavior, what do you think @felixarntz?
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.
@adamsilverstein When is the $context false here? Does the post content in a block theme not go through the_content?
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.
when a block theme is enabled, the_content filter is applied to the content with $context false which appears to happen before the main application of the filter. I'm going to dig a bit further to see why that is happening, maybe we can avoid it in the first place.
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.
Probably this:
| $content = apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) ); |
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.
@adamsilverstein Hmm that code doesn't look odd to me. The $context is never explicitly specified in that filter, it uses current_filter() by default, which means here it should be set to the_content. This would only be a problem if some core code called wp_filter_content_tags() directly without the parameter (i.e. outside of a filter).
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.
indeed I verified that isn't the problem, I'm going to dig in more to trace back and find where the false context value is coming from.
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.
@felixarntz I dug in a bit further here...
wp_filter_content_tags is called several times by block themes in two places
: in src/wp-includes/blocks/template-part.php::render_block_core_template_part with no context and current_filter returning false.
Removing this call fixed the issue (but I don't think we can do that):
| $content = wp_filter_content_tags( $content ); |
note: also called in src/wp-includes/block-template.php in get_the_block_template_html:
| $content = wp_filter_content_tags( $content ); |
I considered some other fixes but the check in the current PR seemed the most reliable, what do you think?
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.
Sample trace:
wp_filter_content_tags (/wp-includes/media.php:1782)
render_block_core_template_part (/wp-includes/blocks/template-part.php:137)
WP_Block->render (/wp-includes/class-wp-block.php:256)
render_block (/wp-includes/blocks.php:1051)
do_blocks (/wp-includes/blocks.php:1089)
get_the_block_template_html (/wp-includes/block-template.php:240)
include (/wp-includes/template-canvas.php:12)
require_once (/wp-includes/template-loader.php:106)
require (/wp-blog-header.php:19)
{main} (/index.php:17)
|
@adamsilverstein I gave this another look, but I think #3560 is a bit of a cleaner solution as it relies on providing new contexts where they're missing today, and also doesn't disable lazy-loading completely for these contexts. The resulting behavior today would be the same, but as we are likely to further enhance the lazy-loading default heuristics later, I think it's better to handle all that in the |
|
Closing in favor of #3560. |
wp_lazy_loading_enabledby default when context is false. Prevents counting of the fist block theme content pass which resulted in actual content filtering setting all content images loading attribute tolazyTesting instructions:
twentytwentythreethemeloading="lazy"attributeloading="lazy"attributetwentyninetween) to verify the same expected behaviorTrac ticket: https://core.trac.wordpress.org/ticket/56930
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.