Skip to content
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

PLANET-6742 Add right sizes attributes for some core blocks #837

Merged
merged 7 commits into from
Apr 26, 2022

Conversation

Inwerpsel
Copy link
Contributor

@Inwerpsel Inwerpsel commented Apr 25, 2022

Ref: PLANET-6742

We found while checking the Brasil site that image handling in core is still not great, not just on the Query Loop block. With some small fixes we can reduce the amount of unnecessary data being sent by a lot.

These 2 blocks are relatively easy to provide the right sizes attribute, which helps browsers avoid downloading oversized images. Image handling is still an open issue in core and doesn't seem to have much progress lately.

Investigating:

  • Is the replacement safe to do? Should fail "gracefully" the fix would just not be applied, visual result is identical.
  • It gets hard when multiple blocks that force a width are nested in each other. In that case it might be possible to access block context, at least for a few cases. For now this is not handled, only top level Media and Text / Query Loop grid will have proper sizes.

How I tested these changes

https://github.com/ausi/respimagelint can scan a page's images and report on missing/incorrect data in srcset and sizes attributes.

On the master branch, running this bookmarklet clearly shows errors of images being multiple times smaller than their sizes attributes indicates.
Screenshot from 2022-04-25 14-15-41

On this branch, it should not give any more errors for images inside Query Loop (grid) and Media and Text. (page is identical)

Screenshot from 2022-04-25 14-17-01

Media and Text

This one is easier and likely has more benefits. However if the block is inside a container that forces a width, it's still way off. At least page header images.

Query Loop

  • Makes the assumption that the only images inside the block are post featured images spanning the full width of the container.
  • It gets hard when multiple blocks that force a width are nested in each other. In that case it might be possible to access block context, at least for a few cases.

@Inwerpsel Inwerpsel force-pushed the core-images-sizes branch 2 times, most recently from 0e4df68 to 764c65b Compare April 25, 2022 11:25
planet-4 added a commit to greenpeace/planet4-test-oberon that referenced this pull request Apr 25, 2022
/unhold 9d24d40b-6e49-4e42-bd02-473c8d3f4756
@Inwerpsel Inwerpsel changed the title Improve core Query Loop sizes attributes Add right sizes attributes for some core blocks Apr 25, 2022
@planet-4
Copy link
Contributor

planet-4 commented Apr 25, 2022

Test instance is ready 🚀

🌑 oberon | admin | blocks report | CircleCI | composer-local.json

⌚ 2022.04.26 14:57:49

@Inwerpsel Inwerpsel changed the title Add right sizes attributes for some core blocks PLANET-6742 Add right sizes attributes for some core blocks Apr 25, 2022
$sizes_attr = 'sizes="' . implode( ', ', $sizes ) . ', 100vw"';

// Assume all images are full width in a container.
$block_content = preg_replace( '/sizes=".*"/', $sizes_attr, $block_content );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is safe to do as long as the Query Loop block has no other images than the post featured image. This is the case for now, and the result shouldn't be that bad if other image are affected.

Perhaps we can make assumptions about the sizes attributes we're matching to get more certainty. e.g. match only those that default to 100vw.

planet-4 added a commit to greenpeace/planet4-test-oberon that referenced this pull request Apr 25, 2022
/unhold 37862df1-174e-4efb-8f5c-25ea099a038c
planet-4 added a commit to greenpeace/planet4-test-oberon that referenced this pull request Apr 25, 2022
/unhold 6eb1fc4e-ed42-4681-a93c-d50171b2fa45
planet-4 added a commit to greenpeace/planet4-test-oberon that referenced this pull request Apr 25, 2022
/unhold 3cf6119d-1d60-45ff-b736-04a3fbe7560c
planet-4 added a commit to greenpeace/planet4-test-oberon that referenced this pull request Apr 25, 2022
/unhold 669da4da-f46d-4c45-96f5-a64e5dc932ed
planet-4 added a commit to greenpeace/planet4-test-oberon that referenced this pull request Apr 25, 2022
/unhold 8f2188b2-af23-4d72-bdcd-6eccbb9f46ea
planet-4 added a commit to greenpeace/planet4-test-oberon that referenced this pull request Apr 25, 2022
/unhold 5828594f-d6e3-412f-9f13-12eeafa8888c
@Inwerpsel
Copy link
Contributor Author

Inwerpsel commented Apr 25, 2022

Interestingly desktop benefits more from this. On mobile the images affected here are shown almost full screen width, so the srcset was already not that far off.

On desktop however....
before
Screenshot from 2022-04-25 18-54-22

after
Screenshot from 2022-04-25 18-58-17

* Makes the assumption that the only images inside the block are post
featured images spanning the full width of the container.
* A similar fix can be done for media and text, and possibly columns.
* It gets hard when multiple blocks that force a width are nested in
each other. In that case it might be possible to access block context,
at least for a few cases.
* It's a different case from the Query Loop block, which already has
sizes and srcset attribute. Media and Text relies and a filter running
after it to add the right things. However if the filter already finds
both attributes, it leaves them in place.
* Support media width attribute, as it's always a percentage of the
viewport width.
* Didn't properly calculate paddings.
planet-4 added a commit to greenpeace/planet4-test-oberon that referenced this pull request Apr 26, 2022
/unhold 41ddc8e2-f3fb-496b-9533-2d4c0d5edad7
@Inwerpsel
Copy link
Contributor Author

Inwerpsel commented Apr 26, 2022

The image linting now completely passes on each variant of Media and Text, and Query Loop with any amount of items per row.

You can validate by running respimagelint on these pages. You can see the impact of the changes by opening the page with a nofix=true query param, to see how many image issues the pages originally have.
https://www-dev.greenpeace.org/test-oberon/media-and-text-sizes/
https://www-dev.greenpeace.org/test-oberon/sizes-attribute/

@Inwerpsel Inwerpsel self-assigned this Apr 26, 2022
@Inwerpsel Inwerpsel force-pushed the core-images-sizes branch 2 times, most recently from e8717ea to 7b7f9af Compare April 26, 2022 10:54
@Inwerpsel Inwerpsel marked this pull request as ready for review April 26, 2022 10:55
planet-4 added a commit to greenpeace/planet4-test-oberon that referenced this pull request Apr 26, 2022
/unhold c3e0034b-1ca3-4919-af35-82c8aec8c880
'width' => '540px',
],
[
'screen' => 577,
Copy link
Contributor

@GP-Dan-Tovbein GP-Dan-Tovbein Apr 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong, but if this value refers to min-width, because of this, which is the value for lower screens?

Copy link
Contributor Author

@Inwerpsel Inwerpsel Apr 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For lower screens it goes to full width of the screen - padding (or no padding for full width blocks). I didn't add it here yet because the default is a "special case" where the width is not specified.

Copy link
Contributor

@GP-Dan-Tovbein GP-Dan-Tovbein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me!
I've left some comments that could be reviewed in case of them apply.

planet4-gutenberg-blocks.php Show resolved Hide resolved
planet4-gutenberg-blocks.php Show resolved Hide resolved
* The padding is there if the whole Query Loop block isn't set as full
width. If it is set as full width, the image size calculation is very
different.
* Already handled by default case.
* Now it gives right sizes for both.
* Improve breakpoint for change from full width to not full width.
planet-4 added a commit to greenpeace/planet4-test-oberon that referenced this pull request Apr 26, 2022
/unhold 7619cd9d-8468-4009-8fbf-befc667fe8db
@Inwerpsel Inwerpsel merged commit ef9cb0e into master Apr 26, 2022
@Inwerpsel Inwerpsel deleted the core-images-sizes branch April 26, 2022 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants