-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
See if its possible to calculate image 'sizes' attribute inside of Gutenberg #46987
Comments
I'm just checking this out hastily but see https://core.trac.wordpress.org/ticket/45407 |
@skorasaurus Ah! Yeah, that's exactly in the same space. @mor10 Any thoughts on ^? |
@danielbachhuber last I heard @mor10 wasn't contributing recently. Just an FYI as he was spearheading a lot of the image discussions in 2018/2019. Can you expand on what you think would be needed to start bringing this proposal to life? Since I also create custom sizes definitions for my clients, I'm pretty invested in how this could be possible with blocks! |
@graylaurenm I think we need a reasonably strong JavaScript developer to spend a day hacking on it. I'm not 100% confident how feasible it is — a day of effort would probably give us a sense of "yes, this seems possible" vs. "uh oh, there are problems". |
I'm definitely not that developer, but I'm interested in understanding this approach enough to find that person. For the moment, I'm going to mention #6177 because it–and Trac 45407 above–are the two tickets everything about srcset sizes ultimately comes back to. So I want to link this up. |
@graylaurenm Yep, makes sense! |
The challenge here, as it has been from the start, is the only entity with knowledge of the final rendered width of an image is the current active theme and block. The browser cannot know this, which is why the With themes, the solution is to provide the developer with the means to specify max displayed widths and breakpoints for image areas as I proposed in 45407. The natural extension of this is blocks inheriting and respecting these values dynamically. That way if and when the site owner changes their theme, the max sizes of images in blocks change accordingly. This in turn requires the I suggest reaching out to @yoavweiss to get first-hand knowledge of how responsive images are rendered at the browser level. He might also be able to point you towards people who are working on large scale solutions right now. Also worth looking at is CDN solutions like Cloudinary, though even there the tools require the site providing info about max displayed widths for each image. |
@mor10 I feel like I'm missing something obvious but I don't know what it is 😞 If the block editor can emulate a range of potential viewport widths, why can't it determine what the correct |
For each image the sizes="(min-width: 66em) 33vw,
(min-width: 44em) 50vw,
100vw" Read from the bottom this translates as
Based on this info, the browser checks the https://web.dev/learn/design/responsive-images/#sizes The intention is to give the developer capabilities to load the smallest possible image for any layout. For example, in a responsive layout with a sidebar, the image width at a narrow viewport may be wider than the image width in a wider viewport because the sidebar reduces the layout space for the image. The This is one of the Very Sticky Problems with the whole RICG spec and why its rollout has been so challenging: it runs counter to how we understand RWD and how the rest of the web platform works. And to top it all off, the spec often ends up counteracting its original purpose - to reduce image sizes on smaller screens - because smaller screens now often have extremely high resolution and therefore pull huge images when the The current best-practice recommendation for My recommendation in general remains what it was back in the day: Have the theme set a global max width for regular content and default the No matter which way you turn this particular Gordian knot, the end result will always be to somehow dynamically generate the |
Thanks for the detailed explanation, @mor10. I appreciate you taking the time to weigh in.
I guess I'm being dense but I still don't understand why some JavaScript can't inspect the actual image size after layout (across a variety of viewports), and then use this data to provide a suggested
This seems like it could be a nice win 😊 |
If I understand you correctly you want to pre-render the page upon save, inspect the layout, then populate the sizes attributes accordingly? If so, then yes, sure, that could work, if the script runs through all possible viewport widths to find the breakpoints etc. I'm all for practicality, and rather than make some overtly complex system I recommend doing the simplest possible thing which is to 1) identify the max-width of the main layout area for the theme, 2) make that the max-width for all images (with exceptions for full-bleed), and 3) configure a standard set of progressive image sizes (small, medium, large, xl, etc) that are always the same across all themes for consistency. That way you don't have to regenerate custom image sizes every time a new theme is added. Something like this: <img
src="image-large.png"
alt="A description of the image."
width="300"
height="200"
loading="lazy"
decoding="async"
srcset="image-thumb.png 150w,
image-small.png 300w,
image-medium.png 800w,
image-large.png 1200w,
image-xl.png 1800w"
sizes="(min-width: [$theme_breakpoint]) [$theme_max_width], 100vw"
> |
Cool, makes sense on both accounts 👍 To help decide "how perfect do we need |
A straight-forward way of testing this:
|
On this topic I tried out a small hack project this weekend to take another approach which is to separate the dependency on It's ~500B of JS, a progressive enhancement, using a
Very ugly demo site here https://gigantic-three-sparrow.glitch.me/ If I get some time I'll try and do the maths per @mor10's comment, but it's not my strong suit! |
If I were a betting man, I'd bet that the
sizes
attribute is wrong for most images on WordPress sites. A correctsizes
attribute is really important for the browser to know whichsrcset
image to download.RespImageLint (https://github.com/ausi/respimagelint) is a nifty bookmarklet to lint your
sizes
andsrcset
values. It would be neat if we could reuse its logic to calculatesizes
attributes inside of Gutenberg.However, I have no idea if this is technically possible! Before we commit to such a feature, we should evaluate whether it's feasible and what might be involved.
Done is:
sizes
attributes for images.The text was updated successfully, but these errors were encountered: