#59352 closed defect (bug) (fixed)
Inline images inserted in the block editor can erroneously get fetchpriority=high
Reported by: | westonruter | Owned by: | flixos90 |
---|---|---|---|
Milestone: | 6.5 | Priority: | normal |
Severity: | normal | Version: | 6.3 |
Component: | Media | Keywords: | has-patch has-unit-tests commit |
Focuses: | performance | Cc: |
Description (last modified by )
I found that inserting an inline image in the block editor can result in it erroneously getting fetchpriority=high
even though it is normally rendered as a small image. When inserting an inline image, the default size (width) is 150 pixels. Nevertheless, the originally uploaded image is actually chosen for the src
(along with its width
and height
) and the user-supplied size is added as an inline width
CSS style. Here is the rendered markup for an inline image appearing as the first image in the content:
<img decoding="async" fetchpriority="high" width="534" height="534" class="wp-image-143" style="width: 30px" src="http://example.org/wp-content/uploads/2023/09/u1f629_u1f973.png" alt="Anguished partygoer emoji" srcset=" http://example.org/wp-content/uploads/2023/09/u1f629_u1f973.png 534w, http://example.org/wp-content/uploads/2023/09/u1f629_u1f973-300x300.png 300w, http://example.org/wp-content/uploads/2023/09/u1f629_u1f973-150x150.png 150w " sizes="(max-width: 534px) 100vw, 534px" />
The fetchpriority=high
attribute is added to this image even when it is immediately followed by an image block with a large size.
To address this, wp_get_loading_optimization_attributes()
could take into account $attr['style']
and parse out any width
property which would override $attr['width']
. This should address inline images from the block editor, but note it would not account for images that are resized by CSS style rules.
Aside: The sizes
attribute here, at least in Twenty Twenty-Three, is causing the full size image to be rendered even though the thumbnail size is included in the srcset
. It seems like whatever the user supplied as the size of the image (e.g. 30px here) should be mirrored between the inline style
attribute's width
and the sizes
attribute. So here it could be sizes="30px"
.
Attachments (3)
Change History (17)
#1
follow-up:
↓ 3
@
16 months ago
It seems to me like the real bug here is that an inline style is being added to control the width of this image instead of updating the width attribute. This also means that WP is incorrectly creating a sizes
attribute based on the width attribute, which is being overridden by the inline CSS.
I'd suggest that we look into fixing the markup generated by the inline image component, rather than adjusting optimizations to take these inline styles into account as a first step.
#3
in reply to:
↑ 1
@
16 months ago
Replying to joemcgill:
It seems to me like the real bug here is that an inline style is being added to control the width of this image instead of updating the width attribute. This also means that WP is incorrectly creating a
sizes
attribute based on the width attribute, which is being overridden by the inline CSS.
Good point. I made a POC plugin to fix up the issue using HTML Tag Processor: https://gist.github.com/westonruter/f37f747a57643462b7efe110dd61d91b
To fix this in core, it seems like this would need to be a special case in wp_img_tag_add_width_and_height_attr()
, yeah?
#4
@
15 months ago
- Milestone changed from Future Release to 6.5
- Owner set to flixos90
- Status changed from new to assigned
Tentatively moving this to the 6.5 milestone for further investigation.
#5
@
13 months ago
I agree that the root of the problem is in the inline image component in Gutenberg. That said, it could be fixed either way. The existing wp_img_tag_add_width_and_height_attr()
function was originally introduced as a fallback to add missing dimension attributes already partly because Gutenberg wasn't always adding those attributes, so I think adding a special condition for this use case could work.
Doing it in that function is also more reliable as it will address the problem for existing content, while doing it in Gutenberg would likely only fix it for new content created in the future.
This ticket was mentioned in PR #5767 on WordPress/wordpress-develop by @flixos90.
13 months ago
#6
- Keywords has-patch added; needs-patch removed
See https://core.trac.wordpress.org/ticket/59352#comment:3: This adds an extra condition to wp_img_tag_add_width_and_height_attr()
which ensures the dimension attribute respects an enforced width via style
attribute.
Trac ticket: https://core.trac.wordpress.org/ticket/59352
@westonruter commented on PR #5767:
13 months ago
#7
I just tested this PR with Playground and it worked. Wow, that is a nice way to test.
Editor:
Post content:
This is inline: [[Image(https://i0.wp.com/playground.wordpress.net/scope:0.6233595411696027/wp-content/uploads/2023/12/anguished-partygoer.png)]]
Frontend HTML:
This is inline: [[Image(https://i0.wp.com/playground.wordpress.net/scope:0.6233595411696027/wp-content/uploads/2023/12/anguished-partygoer.png)]]
The width
and height
attributes are added.
This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.
13 months ago
@flixos90 commented on PR #5767:
12 months ago
#9
@westonruter @joemcgill I just added test coverage (see updated PR description). Given you already approved, I'm planning to commit this tomorrow, but please take a look at the tests if you can.
This ticket was mentioned in Slack in #core-performance by flixos90. View the logs.
12 months ago
@flixos90 commented on PR #5767:
12 months ago
#13
Committed in https://core.trac.wordpress.org/changeset/57294
Anguished partygoer emoji, courtesy of [emoji kitchen] on Google.com