You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Visual tool to scan a page and report missing width/height elements #10
@adamsilverstein before I move onto this ticket - do you have any thoughts with regards to implementation? Is this something that we would invision living in WordPress, or separate, like a chrome extension?
Just to note that sometimes authors intentionally omit width/height attributes such as when adding an remote image that changes randomly. See #49 (comment). Surely not the normal case, but I just wanted to mention the use case.
@felixarntz@ThierryA@adamsilverstein@AymenLoukil - I have a minor proof of concept ready here, but I just want to make sure that this is something we actively want to pursue, considering where we currently are with images overall? This feature is blurring the lines between what Lighthouse does pretty well already in terms of flagging images, so I guess my question is - should we rely on Lighthouse and will users know to use it? Or should we looking into incorporating this tool into the plugin?
@swissspidy Hummm. Yes! Beyond just reporting issues with missing width and height on elements, it can measure the dimensions of those elements and supply the missing width and height attributes during optimization. 🎉
In the case where an image lacks width/height due to the loaded image having variable dimensions, it will be important that we collect multiple URL metrics for a URL before attempting to supply missing width and height attributes. Only once we have multiple URL metrics and the element ends up with the same width/height across each page load can we safely supply the dimensions.
I think detect.js may need to be extend to obtain the intrinsic dimensions of elements that have them: img, video, canvas, svg (maybe), and input[type=image] (ewwww).
With these stored in URL metrics, the optimization pass would then set the missing width and height attributes to match. Another custom attribute should also be added like data-od-using-intrinsic-dimensions so that the detection logic knows it should continue to obtain the intrinsic dimensions for storage, if it isn't doing so already.
Related to this, I just learned that VIDEO tags in the Video block are not inserted with width and height attributes, which means they can cause layout shift. See WordPress/gutenberg#52185.
Note what happens when a landscape image is set as the poster:
Screen.recording.2024-10-15.14.06.56.webm
All of these layout shifts would be eliminated if we supplied the width and height on the VIDEO to correspond to the videoWidth and videoHeight as discovered client-side.
I've also filed #1592 for setting appropriate preload values based on whether a video is in the initial viewport. (Note: I initially commented here in error that the default preload value is auto when in reality it is up to the browser, but the spec encourages metadata as the default.)
Here's an example of the impact of this prototype Intrinsic Dimensions plugin when used with a Video block, here specifically a portrait video which did not have a poster image supplied.
Before
After
Screen.recording.2025-01-19.13.50.37.webm
Screen.recording.2025-01-19.13.51.52.webm
As you can see, the elimination of the layout shift is a dramatic improvement in the page experience. It reduces CLS from poor to good:
My assumption was that WordPress wasn't able to determine the dimensions of video files, but I was wrong. It turns out that wp_generate_attachment_metadata() has explicit support for obtaining video metadata via wp_read_video_metadata(). So this makes the use of Optimization Detective to measure the intrinsic dimensions on frontend much less important, since videos uploaded to WordPress should have their dimensions available. We just need to update the Video block in Gutenberg to supply the dimensions and the aspect-ratio, as implemented in the tag visitor:
// Set the width and height to reflect the captured intrinsic dimensions.$processor->set_attribute( 'width', (string) $intrinsic_dimensions['width'] );
$processor->set_attribute( 'height', (string) $intrinsic_dimensions['height'] );
if ( 'VIDEO' === $processor->get_tag() ) {
// TODO: It's not clear why the aspect-ratio needs to be specified when the user agent style is already defining `aspect-ratio: auto $width / $height;`.// TODO: Also, the Video block has styles the VIDEO with width:100% but it lacks height:auto. Why?// TODO: Why does the Image block use width:content-fit?$style = sprintf( 'height: auto; width: 100%%; aspect-ratio: %d / %d;', $intrinsic_dimensions['width'], $intrinsic_dimensions['height'] );
$old_style = $processor->get_attribute( 'style' );
if ( is_string( $old_style ) ) {
$style .= $old_style;
}
$processor->set_attribute( 'style', $style );
}
Where Optimization Detective is helpful is obtaining dimensions for videos (and images) which are loaded externally.
I've opened WordPress/gutenberg#70293 to supply the width and height to the video tag in the Video block when the metadata is available. The changes in this PR are also available in a standalone plugin for existing sites to use before the change is published in a Gutenberg release: Layout-stabilized Video Block.
Activity
eclarke1 commentedon Jan 17, 2022
@adamsilverstein I'll add this to the measurement focus for now and into the backlog, but let me know if that should change.
dainemawer commentedon Jan 20, 2022
@eclarke1 @adamsilverstein I think I could take a stab at this - sounds like a fun feature to build out.
eclarke1 commentedon Jan 20, 2022
Thanks @dainemawer we can assign this issue to you then and it would be great to move over to 'In Progress' once this is started please
dainemawer commentedon Jan 24, 2022
@adamsilverstein before I move onto this ticket - do you have any thoughts with regards to implementation? Is this something that we would invision living in WordPress, or separate, like a chrome extension?
westonruter commentedon Jan 24, 2022
Just to note that sometimes authors intentionally omit
width/heightattributes such as when adding an remote image that changes randomly. See #49 (comment). Surely not the normal case, but I just wanted to mention the use case.dainemawer commentedon Mar 7, 2022
@felixarntz @ThierryA @adamsilverstein @AymenLoukil - I have a minor proof of concept ready here, but I just want to make sure that this is something we actively want to pursue, considering where we currently are with images overall? This feature is blurring the lines between what Lighthouse does pretty well already in terms of flagging images, so I guess my question is - should we rely on Lighthouse and will users know to use it? Or should we looking into incorporating this tool into the plugin?
swissspidy commentedon May 10, 2024
@westonruter Just stumbled upon this — maybe a candidate for Optimization Detective?
westonruter commentedon May 10, 2024
@swissspidy Hummm. Yes! Beyond just reporting issues with missing width and height on elements, it can measure the dimensions of those elements and supply the missing
widthandheightattributes during optimization. 🎉7 remaining items
westonruter commentedon Jun 22, 2024
In the case where an image lacks width/height due to the loaded image having variable dimensions, it will be important that we collect multiple URL metrics for a URL before attempting to supply missing
widthandheightattributes. Only once we have multiple URL metrics and the element ends up with the same width/height across each page load can we safely supply the dimensions.westonruter commentedon Oct 11, 2024
As noted in #1420:
So for an image, I believe this would be by getting the
naturalWidth&naturalHeight, and for video it would be thevideoWidthandvideoHeightproperties.With these stored in URL metrics, the optimization pass would then set the missing
widthandheightattributes to match. Another custom attribute should also be added likedata-od-using-intrinsic-dimensionsso that the detection logic knows it should continue to obtain the intrinsic dimensions for storage, if it isn't doing so already.westonruter commentedon Oct 15, 2024
Related to this, I just learned that
VIDEOtags in the Video block are not inserted withwidthandheightattributes, which means they can cause layout shift. See WordPress/gutenberg#52185.westonruter commentedon Oct 15, 2024
Here's an example portrait video on a 3G connection without
preloadspecified and noposter:Screen.recording.2024-10-15.13.52.38.webm
And with a
postersupplied, in which case the poster image dimensions become the video's initial intrinsic dimensions:Screen.recording.2024-10-15.13.56.46.webm
And without
posterand withpreload=none:Screen.recording.2024-10-15.13.59.54.webm
Note what happens when a landscape image is set as the
poster:Screen.recording.2024-10-15.14.06.56.webm
All of these layout shifts would be eliminated if we supplied the
widthandheighton theVIDEOto correspond to thevideoWidthandvideoHeightas discovered client-side.I've also filed #1592 for setting appropriate
preloadvalues based on whether a video is in the initial viewport. (Note: I initially commented here in error that the defaultpreloadvalue isautowhen in reality it is up to the browser, but the spec encouragesmetadataas the default.)westonruter commentedon Dec 24, 2024
I'm exploring this in the context of a separate repo at the moment, although I intend to open a PR to implement it as part of Image Prioritizer once it is stable: https://github.com/westonruter/od-intrinsic-dimensions
westonruter commentedon Jan 19, 2025
Here's an example of the impact of this prototype Intrinsic Dimensions plugin when used with a Video block, here specifically a portrait video which did not have a poster image supplied.
Screen.recording.2025-01-19.13.50.37.webm
Screen.recording.2025-01-19.13.51.52.webm
As you can see, the elimination of the layout shift is a dramatic improvement in the page experience. It reduces CLS from poor to good:
westonruter commentedon Feb 20, 2025
My assumption was that WordPress wasn't able to determine the dimensions of video files, but I was wrong. It turns out that
wp_generate_attachment_metadata()has explicit support for obtaining video metadata viawp_read_video_metadata(). So this makes the use of Optimization Detective to measure the intrinsic dimensions on frontend much less important, since videos uploaded to WordPress should have their dimensions available. We just need to update the Video block in Gutenberg to supply the dimensions and theaspect-ratio, as implemented in the tag visitor:Where Optimization Detective is helpful is obtaining dimensions for videos (and images) which are loaded externally.
swissspidy commentedon Feb 20, 2025
Not sure how reliable
wp_read_video_metadata()really is as it relies on metadata. Might not always be available.westonruter commentedon Jun 3, 2025
I've opened WordPress/gutenberg#70293 to supply the
widthandheightto thevideotag in the Video block when the metadata is available. The changes in this PR are also available in a standalone plugin for existing sites to use before the change is published in a Gutenberg release: Layout-stabilized Video Block.westonruter commentedon Jun 5, 2025
I blogged a writeup about Eliminating Layout Shifts in the Video Block.