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 2 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
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
38 changes: 37 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 @@ -189,6 +196,9 @@ class PreloadHeroImage {
if (this.isTinyNode(layout, width, height)) {
return null;
}
if (this.experimentImg) {
this.generateImg(ampImg);
}
return {src, srcset, media};
}

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

generateImg(node) {
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,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>