Add support for IMG tags with fetchpriority=low or fetchpriority=auto#11196
Add support for IMG tags with fetchpriority=low or fetchpriority=auto#11196westonruter wants to merge 31 commits intoWordPress:trunkfrom
IMG tags with fetchpriority=low or fetchpriority=auto#11196Conversation
|
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. |
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. |
There was a problem hiding this comment.
Pull request overview
Updates core loading-optimization heuristics to treat img[fetchpriority=low] as a special case so it won’t be lazy-loaded, won’t affect the content media count, and won’t interfere with assigning fetchpriority=high to the first in-viewport large image.
Changes:
- Extend
wp_get_loading_optimization_attributes()to recognize existingfetchpriority=lowand avoid addingloading="lazy"/ incrementing media count. - Add PHPUnit coverage to ensure
fetchpriority=lowimages don’t influence the “first N images not lazy-loaded” and high-priority selection logic.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/wp-includes/media.php |
Adds fetchpriority=low handling in loading optimization logic and documents the behavior. |
tests/phpunit/tests/media.php |
Adds helper + additional assertions to cover fetchpriority=low interactions across contexts/loops. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>
Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>
IMG with fetchpriority=low does not get lazy-loaded, increase media count, or interfere with assigning fetchpriority=highIMG tags with fetchpriority=low or fetchpriority=auto
|
@mukeshpanchal27 I've expanded the scope here to include the closely related changes for block visibility described in WordPress/gutenberg#76301. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 8 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
| * } | ||
| * @return bool Whether a tag was matched. | ||
| * | ||
| * @phpstan-impure |
There was a problem hiding this comment.
See https://phpstan.org/writing-php-code/phpdocs-basics#impure-functions
This is needed so that $processor->next_tag() isn't flagged by PHPStan in the above.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
33b17a5 to
dcf1ecb
Compare
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /* | ||
| * Set all IMG tags to be `fetchpriority=auto` so that wp_get_loading_optimization_attributes() won't add | ||
| * `fetchpriority=high` or increment the media count to effect whether subsequent IMG tags get `loading=lazy`. | ||
| */ | ||
| do { | ||
| if ( 'IMG' === $processor->get_tag() ) { | ||
| $processor->set_attribute( 'fetchpriority', 'auto' ); | ||
| } | ||
| } while ( $processor->next_tag() ); |
There was a problem hiding this comment.
This is a backport from WordPress/gutenberg#76302
There was a problem hiding this comment.
This function is for “block visibility” support, but it’s now also altering image-loading heuristics globally for any image inside the block. That may be intended, but it’s something that it extend the scope of this function. In future we need to check better place for it. WDYT?
There was a problem hiding this comment.
This is the context in which we know that an IMG will be conditionally displayed based on the viewport size. There isn't any other code I'm aware of where PHP is aware of such styles being applied. So I don't think there is currently another spot.
There was a problem hiding this comment.
For visibility: this change was approved in the the corresponding Gutenberg PR by @t-hamano and @ramonjd.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…crementing media count for them
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…cks (#76336) * Add fetchpriority=low to IMG tags in collapsed Accordion Item blocks * Remove removal of loading attribute since handled by WordPress/wordpress-develop#11196 Co-authored-by: westonruter <westonruter@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: im3dabasia <im3dabasia1@git.wordpress.org>
…cks (#76336) * Add fetchpriority=low to IMG tags in collapsed Accordion Item blocks * Remove removal of loading attribute since handled by WordPress/wordpress-develop#11196 Co-authored-by: westonruter <westonruter@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: im3dabasia <im3dabasia1@git.wordpress.org>
…cks (#76336) * Add fetchpriority=low to IMG tags in collapsed Accordion Item blocks * Remove removal of loading attribute since handled by WordPress/wordpress-develop#11196 Co-authored-by: westonruter <westonruter@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: im3dabasia <im3dabasia1@git.wordpress.org> Source: WordPress/gutenberg@50a3fc5
…cks (#76336) * Add fetchpriority=low to IMG tags in collapsed Accordion Item blocks * Remove removal of loading attribute since handled by WordPress/wordpress-develop#11196 Co-authored-by: westonruter <westonruter@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: im3dabasia <im3dabasia1@git.wordpress.org> Source: WordPress/gutenberg@947d78a
Trac ticket: https://core.trac.wordpress.org/ticket/64823
Ensure
IMGwithfetchpriority=lowdoes not get lazy-loaded, increase media count, or interfere with assigningfetchpriority=highWith WordPress/gutenberg#76208 checked out and there are 5 large images added to a Navigation Overlay and 5 large images in the post content, given the following script being run in the console:
Before ❌
[ { "insideNavOverlay": true, "loading": "auto", "fetchPriority": "low" }, { "insideNavOverlay": true, "loading": "auto", "fetchPriority": "low" }, { "insideNavOverlay": true, "loading": "auto", "fetchPriority": "low" }, { "insideNavOverlay": true, "loading": "auto", "fetchPriority": "low" }, { "insideNavOverlay": true, "loading": "auto", "fetchPriority": "low" }, { "insideNavOverlay": false, "loading": "lazy", "fetchPriority": "auto" }, { "insideNavOverlay": false, "loading": "lazy", "fetchPriority": "auto" }, { "insideNavOverlay": false, "loading": "lazy", "fetchPriority": "auto" }, { "insideNavOverlay": false, "loading": "lazy", "fetchPriority": "auto" }, { "insideNavOverlay": false, "loading": "lazy", "fetchPriority": "auto" } ]After ✅
[ { "insideNavOverlay": true, "loading": "auto", "fetchPriority": "low" }, { "insideNavOverlay": true, "loading": "auto", "fetchPriority": "low" }, { "insideNavOverlay": true, "loading": "auto", "fetchPriority": "low" }, { "insideNavOverlay": true, "loading": "auto", "fetchPriority": "low" }, { "insideNavOverlay": true, "loading": "auto", "fetchPriority": "low" }, { "insideNavOverlay": false, "loading": "auto", "fetchPriority": "high" }, { "insideNavOverlay": false, "loading": "auto", "fetchPriority": "auto" }, { "insideNavOverlay": false, "loading": "auto", "fetchPriority": "auto" }, { "insideNavOverlay": false, "loading": "lazy", "fetchPriority": "auto" }, { "insideNavOverlay": false, "loading": "lazy", "fetchPriority": "auto" } ]The diff shows the changes to the first three images in the content (outside of the navigation overlay):
Fixes:
fetchpriority=highbeing assigned to the first large image outside of the Navigation Overlay.loading=lazyon the first three images in the content.Add support for
fetchpriority=autoThis also improves handling of
IMGtags withfetchpriority=auto, as is proposed solution to prevent viewport-conditional Image blocks from gettingfetchpriority=highapplied. See:fetchpriority=autotoIMGtags in blocks with conditional viewport visibility to prevent potential erroneous high loading priority gutenberg#76302(This wordpress-develop PR is a backport PR for that Gutenberg PR.)
Nevertheless, that PR alone does exclude images with
fetchpriority=autofrom contributing to the overall media count. This means you can have 3 images which are conditionally displayed in mobile, tablet, and desktop, respectively, and then the fourth image is rendered withloading=lazywhen in reality it is only the second image actually displayed.Example Post Content
Before ❌
After ✅
Use of AI Tools
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.