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

add experimental amp-img -> img transformation #859

Merged
merged 9 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/optimizer/lib/DomTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const TRANSFORMATIONS_AMP_FIRST = [
'AutoExtensionImporter',
// Applies image optimizations, must run before PreloadHeroImage
'OptimizeImages',
// Detect hero image and preload link rel=preload
// Detect hero image and preload link rel=preload, needs to run after OptimizeImages
'PreloadHeroImage',
// Applies server-side-rendering optimizations
'ServerSideRendering',
Expand Down
3 changes: 1 addition & 2 deletions packages/optimizer/lib/transformers/ApplyLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ function maybeAddSizerInto(node, layout, width, height) {
sizer = createIntrinsicSizer(width, height);
}
if (sizer) {
const referenceNode = node.firstChild;
insertBefore(node, sizer, referenceNode);
appendChild(node, sizer);
Copy link
Collaborator Author

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.

Copy link
Contributor

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?

Copy link
Collaborator Author

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.

}
}

Expand Down
40 changes: 39 additions & 1 deletion packages/optimizer/lib/transformers/PreloadHeroImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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) {
Expand Down Expand Up @@ -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 || ''};
}
}
Expand Down Expand Up @@ -189,6 +197,7 @@ class PreloadHeroImage {
if (this.isTinyNode(layout, width, height)) {
return null;
}
this.generateImg(ampImg);
return {src, srcset, media};
}

Expand Down Expand Up @@ -216,6 +225,35 @@ class PreloadHeroImage {
}
return {width: 0, height: 0};
}

generateImg(node) {
if (!this.experimentImg) {
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we were only going to do this transform for amp-imgs that have a data-hero attr set. Since we can't guarantee loading=lazy behavior for most browsers yet, we don't want to load a bunch of far-from-viewport images.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Introduce a max upper bound of 2 hero images and switched to loading=eager for these.

const imgNode = createElement('img', {
class: 'class=i-amphtml-fill-content i-amphtml-replaced-content',
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
class: 'class=i-amphtml-fill-content i-amphtml-replaced-content',
class: 'i-amphtml-fill-content i-amphtml-replaced-content',

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ <h1>Hello, AMP world!</h1>
<i-amphtml-sizer style="display:block;padding-top:56.4815%;"></i-amphtml-sizer>
</amp-img>
<amp-video width="480" height="270" src="https://amp.dev/static/samples/video/tokyo.mp4" poster="https://amp.dev/static/samples/img/tokyo.jpg" layout="responsive" controls class="i-amphtml-layout-responsive i-amphtml-layout-size-defined" i-amphtml-layout="responsive">
<i-amphtml-sizer style="display:block;padding-top:56.2500%;"></i-amphtml-sizer>
<div fallback>
<p>Your browser doesn't support HTML5 video.</p>
</div>
<i-amphtml-sizer style="display:block;padding-top:56.2500%;"></i-amphtml-sizer>
</amp-video>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ <h1>Hello, AMP world!</h1>
<i-amphtml-sizer style="display:block;padding-top:56.4815%;"></i-amphtml-sizer>
</amp-img>
<amp-video width="480" height="270" src="https://amp.dev/static/samples/video/tokyo.mp4" poster="https://amp.dev/static/samples/img/tokyo.jpg" layout="responsive" controls class="i-amphtml-layout-responsive i-amphtml-layout-size-defined" i-amphtml-layout="responsive">
<i-amphtml-sizer style="display:block;padding-top:56.2500%;"></i-amphtml-sizer>
<div fallback>
<p>Your browser doesn't support HTML5 video.</p>
</div>
<i-amphtml-sizer style="display:block;padding-top:56.2500%;"></i-amphtml-sizer>
</amp-video>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ <h1>Hello, AMP world!</h1>
<i-amphtml-sizer style="display:block;padding-top:56.4815%;"></i-amphtml-sizer>
</amp-img>
<amp-video width="480" height="270" src="https://amp.dev/static/samples/video/tokyo.mp4" poster="https://amp.dev/static/samples/img/tokyo.jpg" layout="responsive" controls class="i-amphtml-layout-responsive i-amphtml-layout-size-defined" i-amphtml-layout="responsive">
<i-amphtml-sizer style="display:block;padding-top:56.2500%;"></i-amphtml-sizer>
<div fallback>
<p>Your browser doesn't support HTML5 video.</p>
</div>
<i-amphtml-sizer style="display:block;padding-top:56.2500%;"></i-amphtml-sizer>
</amp-video>
</body>
</html>
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>