Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion builtins/amp-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,13 @@ export class AmpImg extends BaseElement {
if (!this.img_) {
return;
}
// If the image is server rendered, do not generate sizes.
if (this.element.hasAttribute('i-amphtml-ssr')) {
Comment thread
kristoferbaxter marked this conversation as resolved.
return;
}
// No need to generate sizes if already present.
const sizes = this.element.getAttribute('sizes');
const sizes =
Comment thread
kristoferbaxter marked this conversation as resolved.
this.element.hasAttribute('sizes') || this.img_.hasAttribute('sizes');
Comment thread
kristoferbaxter marked this conversation as resolved.
if (sizes) {
return;
}
Expand Down
39 changes: 38 additions & 1 deletion test/unit/test-amp-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,44 @@ describes.sandboxed('amp-img', {}, (env) => {
return impl;
}

it('should not generate sizes for amp-imgs that already have sizes', async () => {
it('should not generate sizes for amp-imgs that already have sizes on their rendered image children', async () => {
const serverRenderedImg = document.createElement('img');
serverRenderedImg.setAttribute('src', '/examples/img/sample.jpg');
serverRenderedImg.setAttribute('srcset', SRCSET_STRING);
serverRenderedImg.setAttribute('sizes', '50vw');
const ampImg = await getImg(
{
src: '/examples/img/sample.jpg',
srcset: SRCSET_STRING,
sizes: '50vw',
width: 300,
height: 200,
},
[serverRenderedImg]
);
const impl = await ampImg.getImpl(false);
impl.buildCallback();
await impl.layoutCallback();
const img = impl.img_;
expect(img.getAttribute('sizes')).to.equal('50vw');
});

it('should not generate sizes for amp-imgs when rendered from the server', async () => {
const ampImg = await getImg({
src: '/examples/img/sample.jpg',
srcset: SRCSET_STRING,
width: 300,
height: 200,
'i-amphtml-ssr': '',
});
const impl = await ampImg.getImpl(false);
impl.buildCallback();
await impl.layoutCallback();
const img = impl.img_;
expect(img.hasAttribute('sizes')).to.be.false;
});

it('should not generate sizes for amp-imgs, rendered with sizes from the server', async () => {
const ampImg = await getImg({
src: '/examples/img/sample.jpg',
srcset: SRCSET_STRING,
Expand Down