-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Automatically generate sizes attribute for amp-img #20968
Conversation
7e18c8d
to
ad83b0d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give an an ideal output for the image:
<amp-img alt="Hummingbird"
src="images/hummingbird-wide.jpg"
width="640"
height="457"
srcset="images/hummingbird-wide.jpg 640w,
images/hummingbird-narrow.jpg 320w"
sizes="auto">
</amp-img>
What should sizes
be?
@@ -63,6 +69,13 @@ export class AmpImg extends BaseElement { | |||
} | |||
} | |||
|
|||
/** @override */ | |||
onMeasureChanged() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was the conclusion on the question of:
"If viewport size does NOT change, but sizes
attribute of an img
is updated, do any browser fetch and render a new high res image"?
If no, then we can just do this in viewport-size change event instead of measureChanged
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The answer to that question is yes for Firefox and Chrome, no for Safari (also sizes doesn't update on viewport change or image size change at all ever on Safari). The reason we have this in onMeasureChanged
is because if the image changes size without a viewport change, updating the srcset
will cause a refetch if the image size results in a different srcset being selected.
Re: your question:
Great point. I think I need to rethink what happens when layout is not specified. So, the value of So if we had
This would be interpreted as But if we had I think that is incorrect because I don't think
UPDATE: |
So there's good news and bad news. 👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there. 😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request. Note to project maintainer: This is a terminal state, meaning the ℹ️ Googlers: Go here for more info. |
So sad. I love the commit suggested changes feature, but it doesn't play nice with our CLA bot. I'm going to ahem do some git history rewriting to fix that up now. T.T |
148cb51
to
d7ecae6
Compare
3344b28
to
d51622f
Compare
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
I know I have a merge conflict here, but it's pretty trivial to resolve the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'm confused because there's two separate issues going on:
- What does the a regular img do with sizes? How is amp-img different?
- What do we want to do with sizes="auto"?
We need to fix 1). But I don't think 2) has been well defined yet. At least, I don't know what the correct sizes output is for a given srcset input.
Re: The goal of this PR is as follows:
A bit of background: Browser treatment of
|
builtins/amp-img.js
Outdated
@@ -63,6 +69,13 @@ export class AmpImg extends BaseElement { | |||
} | |||
} | |||
|
|||
/** @override */ | |||
onMeasureChanged() { | |||
if (isExperimentOn(this.getAmpDoc().win, 'amp-img-auto-sizes')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Store this in a private property in constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it ok use this.getAmpDoc()
in the constructor (it currently causes all amp-img tests to fail)? I can stub it if ok, but I've noticed that people generally put this line of code in buildCallback
, but <amp-img>
doesn't use buildCallback
.
I resolved merge conflicts and I believe tidied up all the lose ends on this PR. It should now be ready to merge when CI completes. The plan is to ramp this up gradually as an experiment over 4 weeks until it reaches 100% of prod, and then we'll update our documentation per #21191. |
Sorry @jridgewell could you re-approve for the bundle size bot? |
Thank you! xD |
* Generate sizes if applicable for amp-img * Gate auto-generate sizes under an experiment flag * Add unit tests to for auto-sizes * Fix lint and type check * Fix isExperimentOn call parameter * Add logic to update the sizes attribute onMeasureChanged * Add a manual test case to verify amp-img with auto generated sizes * Fix x descriptor regex * Fix getAmpDoc stub in amp-img tests * Remove debugger statements * Fix regex * Stub ampdoc_ on custom element test because we are using an experiment in this class * Remove extra getLayoutWidth call * Address code review comments * Add early return and store experiment in constructor * Move experiment check to constructor * Unstub getAmpDoc in tests since its no longer used
* Generate sizes if applicable for amp-img * Gate auto-generate sizes under an experiment flag * Add unit tests to for auto-sizes * Fix lint and type check * Fix isExperimentOn call parameter * Add logic to update the sizes attribute onMeasureChanged * Add a manual test case to verify amp-img with auto generated sizes * Fix x descriptor regex * Fix getAmpDoc stub in amp-img tests * Remove debugger statements * Fix regex * Stub ampdoc_ on custom element test because we are using an experiment in this class * Remove extra getLayoutWidth call * Address code review comments * Add early return and store experiment in constructor * Move experiment check to constructor * Unstub getAmpDoc in tests since its no longer used
Experimentally implements #19513.
1. Does not add sizes as a style width if the tag is<amp-img>
.2. If a sizes attribute is not provided to
<amp-img>
, generate one inlayoutCallback
based on its current width.3. In
onMeasureChanged
, if the image is resized to be larger, set a newsizes
attribute.Details at go/amp-img-sizes
Background at go/srcset-and-sizes