-
Notifications
You must be signed in to change notification settings - Fork 243
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
add experimental amp-img -> img transformation #859
Changes from 4 commits
2c7a3be
d695e9f
16c4688
425b9a8
d41daaa
33daf9b
8574987
a3601de
ea594ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,7 +16,13 @@ | |||||
|
||||||
'use strict'; | ||||||
|
||||||
const {createElement, hasAttribute, insertAfter, firstChildByTag} = require('../NodeUtils'); | ||||||
const { | ||||||
appendChild, | ||||||
createElement, | ||||||
hasAttribute, | ||||||
insertAfter, | ||||||
firstChildByTag, | ||||||
} = require('../NodeUtils'); | ||||||
const {findMetaViewport} = require('../HtmlDomHelper'); | ||||||
const {isValidImageSrcURL} = require('../URLUtils'); | ||||||
const {isTemplate} = require('../AmpConstants'); | ||||||
|
@@ -38,6 +44,7 @@ class PreloadHeroImage { | |||||
constructor(config) { | ||||||
this.log = config.log; | ||||||
this.enabled = config.preloadHeroImage !== false; | ||||||
this.experimentImg = config.experimentImg; | ||||||
} | ||||||
async transform(root, params) { | ||||||
if (!this.enabled || params.preloadHeroImage === false) { | ||||||
|
@@ -159,6 +166,7 @@ class PreloadHeroImage { | |||||
hasAttribute(child, 'placeholder') && | ||||||
isValidImageSrcURL(child.attribs.src) | ||||||
) { | ||||||
this.generateImg(child); | ||||||
return {src: child.attribs.src, media, srcset: child.attribs.srcset || ''}; | ||||||
} | ||||||
} | ||||||
|
@@ -189,6 +197,7 @@ class PreloadHeroImage { | |||||
if (this.isTinyNode(layout, width, height)) { | ||||||
return null; | ||||||
} | ||||||
this.generateImg(ampImg); | ||||||
return {src, srcset, media}; | ||||||
} | ||||||
|
||||||
|
@@ -216,6 +225,35 @@ class PreloadHeroImage { | |||||
} | ||||||
return {width: 0, height: 0}; | ||||||
} | ||||||
|
||||||
generateImg(node) { | ||||||
if (!this.experimentImg) { | ||||||
return; | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we were only going to do this transform for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Introduce a max upper bound of 2 hero images and switched to |
||||||
const imgNode = createElement('img', { | ||||||
class: 'class=i-amphtml-fill-content i-amphtml-replaced-content', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||||||
decoding: 'async', | ||||||
loading: 'lazy', | ||||||
}); | ||||||
const attributesToCopy = [ | ||||||
'alt', | ||||||
'attribution', | ||||||
'object-fit', | ||||||
'object-position', | ||||||
'referrerpolicy', | ||||||
'src', | ||||||
'srcset', | ||||||
'sizes', | ||||||
'title', | ||||||
]; | ||||||
for (const attr of attributesToCopy) { | ||||||
if (hasAttribute(node, attr)) { | ||||||
imgNode.attribs[attr] = node.attribs[attr]; | ||||||
} | ||||||
} | ||||||
node.attribs['i-amphtml-ssr'] = ''; | ||||||
appendChild(node, imgNode); | ||||||
} | ||||||
} | ||||||
|
||||||
/** @module PreloadHeroImage */ | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<html> | ||
<head> | ||
<script async src="https://cdn.ampproject.org/v0.js"></script> | ||
<link rel="preload" href="http://example.com/foo.png" as="image" data-hero media="(max-width: 649px)"> | ||
</head> | ||
<body> | ||
<amp-iframe src="/test" layout="responsive" width="320" height="900" media="(max-width: 649px)"> | ||
<amp-img placeholder layout="fill" src="http://example.com/foo.png" i-amphtml-ssr><img class="class=i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" loading="lazy" src="http://example.com/foo.png"></amp-img> | ||
</amp-iframe> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<html> | ||
<head> | ||
<script async src="https://cdn.ampproject.org/v0.js"></script> | ||
</head> | ||
<body> | ||
<amp-iframe src="/test" layout="responsive" width="320" height="900" media="(max-width: 649px)"> | ||
<amp-img placeholder layout="fill" src="http://example.com/foo.png"></amp-img> | ||
</amp-iframe> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<script async src="https://cdn.ampproject.org/v0.js"></script> | ||
</head> | ||
<body> | ||
<amp-img width="500" height="400" alt="Some image" attribution="by someone" object-fit object-position="center" referrerpolicy="unknown" src="foo.jpg" srcset="for2.jpg w320, foo3.jpg" sizes="many" title="the title" i-amphtml-ssr><img class="class=i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" loading="lazy" alt="Some image" attribution="by someone" object-fit object-position="center" referrerpolicy="unknown" src="foo.jpg" srcset="for2.jpg w320, foo3.jpg" sizes="many" title="the title"></amp-img> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<html> | ||
<head> | ||
<script async src="https://cdn.ampproject.org/v0.js"></script> | ||
</head> | ||
<body> | ||
<amp-img width="500" height="400" | ||
alt="Some image" | ||
attribution="by someone" | ||
object-fit="" | ||
object-position="center" | ||
referrerpolicy="unknown" | ||
src="foo.jpg" | ||
srcset="for2.jpg w320, foo3.jpg" | ||
sizes="many" | ||
title="the title" | ||
></amp-img> | ||
</body> | ||
</html> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"experimentImg": true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html> | ||
<head> | ||
<script async src="https://cdn.ampproject.org/v0.js"></script> | ||
<link rel="preload" href="/foo.png" as="image" data-hero> | ||
</head> | ||
<body> | ||
<amp-img width="500" height="400" src="/foo.png" i-amphtml-ssr><img class="class=i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" loading="lazy" src="/foo.png"></amp-img> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<script async src="https://cdn.ampproject.org/v0.js"></script> | ||
</head> | ||
<body> | ||
<amp-img width="500" height="400" src="/foo.png"></amp-img> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html> | ||
<head> | ||
<script async src="https://cdn.ampproject.org/v0.js"></script> | ||
<link rel="preload" href="/foo.png" as="image" data-hero> | ||
</head> | ||
<body> | ||
<amp-img layout="intrinsic" width="500" height="400" src="/foo.png" i-amphtml-ssr><img class="class=i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" loading="lazy" src="/foo.png"></amp-img> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<script async src="https://cdn.ampproject.org/v0.js"></script> | ||
</head> | ||
<body> | ||
<amp-img layout="intrinsic" width="500" height="400" src="/foo.png"></amp-img> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html> | ||
<head> | ||
<script async src="https://cdn.ampproject.org/v0.js"></script> | ||
<link rel="preload" href="/foo.png" as="image" data-hero> | ||
</head> | ||
<body> | ||
<amp-img layout="responsive" width="500" height="400" src="/foo.png" i-amphtml-ssr><img class="class=i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" loading="lazy" src="/foo.png"></amp-img> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<script async src="https://cdn.ampproject.org/v0.js"></script> | ||
</head> | ||
<body> | ||
<amp-img layout="responsive" width="500" height="400" src="/foo.png"></amp-img> | ||
</body> | ||
</html> |
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.
Looks like the sizer needs to be added after the img.
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.
Sizer always goest first for
responsive
. Is there a bug you're seeing when sizer is first?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.
Hm. Not sure what I tested there. All good with sizer first.