Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

SSR: Add support for media, sizes and heights attribute #763

Merged
merged 5 commits into from
May 28, 2020

Conversation

sebastianbenz
Copy link
Collaborator

This will make it possible to remove the boilerplate even if the media, sizes or heights attributes are used. These attributes will be transformed into CSS media queries instead.

@sebastianbenz
Copy link
Collaborator Author

@westonruter @schlessera PTAL - I haven't yet implemented the CSS size check.

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

I reviewed what I could, but @schlessera is better suited to give more in-depth feedback.


const parseSizes = require('../parseSizes');
const {appendChild, createElement, insertText, hasAttribute} = require('../NodeUtils');
const ID_PREFIX = 'i-amp-';
Copy link
Member

Choose a reason for hiding this comment

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

The prefix i-amphtml- is disallowed, but i-amp- is OK. It seems somewhat strange why i-amp- wasn't chosen rather for the internal prefix since it has fewer bytes.

appendChild(head, customStyles);
insertText(customStyles, '');
}
customStyles.children[0].data += styles;
Copy link
Member

Choose a reason for hiding this comment

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

As noted in the description, we'll need to make sure this doesn't go above 75000 bytes.


transform(node, id) {
// normalize whitespace
let mediaString = node.attribs.media.replace(/\s+/g, ' ');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like this is already done within parseSizes() ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

parseSizes is unrelated to the media attribute or am I missing something?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, nvm, I was thinking of the media conditions that are part of the sizes attribute.

*
* @param {Node} node
*/
getOrCreateId(node) {
Copy link
Collaborator

@schlessera schlessera May 15, 2020

Choose a reason for hiding this comment

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

This method doesn't check whether it is maybe creating duplicate IDs. This will break if it runs on markup that is already partially or fully optimized, or which happens to use the same prefix.

The PHP version will detect pre-existing IDs and iterate over them until it finds an ID that is not yet taken: https://github.com/ampproject/amp-wp/blob/63e99a63ca0624d5cff8f6e8d8d1ac85b89e337b/lib/common/src/Dom/Document.php#L1509-L1512

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

True. This is not straightforward to implement in our case unfortunately.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll ignore this case for now. Re-running the transformer would simply re-use the existing id. Pre-existing Ids should not be avoided by our prefix.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

Comment on lines 5 to 7
<noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;
-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes-amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes-amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes-amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes-amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style>
<noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why the change in order here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no idea!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

reverted

Comment on lines +5 to +6


Copy link
Collaborator

Choose a reason for hiding this comment

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

Removal of the boilerplate shouldn't leave empty lines behind.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

that's going to be removed by a different transformer (not used in the test)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This will be fixed by a different transformer

* See https://html.spec.whatwg.org/multipage/images.html#parsing-a-sizes-attribute
*
* @param {String} string
* @returns {!Sizes}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why the "non-nullable" modifier ! here specifically? Wouldn't that be the default without the modifier anyways? It seems like that distinction isn't use anywhere else.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's a closure compiler flag used by the AMP runtime. Removed it as we don't need it.

@sebastianbenz
Copy link
Collaborator Author

I've addressed the comments. In regards to checking for the CSS limit. I'm currently lending towards not including this check:

  • CSS optimization would need to be run twice (before, to be able to get the correct byte count and after, to optimize the generated CSS rules).
  • I'd prefer to fail on not being able to remove the boilerplate, the perf implications of not being able to remove the boilerplate because of this are a lot harder to detect otherwise.

@sebastianbenz
Copy link
Collaborator Author

@westonruter @schlessera PTAL

@westonruter
Copy link
Member

In regards to checking for the CSS limit. I'm currently lending towards not including this check

Doesn't this run the risk of the Optimizer generating invalid AMP from valid AMP input? If the CSS is close to the limit and then optimization causes it to go over, that seems not ideal?

Copy link
Collaborator Author

@sebastianbenz sebastianbenz left a comment

Choose a reason for hiding this comment

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

Optimizer changes CSS for the better and for the worse. This means you should run validation afterwards. I'm OK with failing validation if CSS impacts perf.

Comment on lines +5 to +6


Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This will be fixed by a different transformer

*
* @param {Node} node
*/
getOrCreateId(node) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

@sebastianbenz
Copy link
Collaborator Author

Merging as there's no further feedback that needs to be addressed.

@schlessera
Copy link
Collaborator

@sebastianbenz There seems to be an issue with the CSS in here that was raised twice but always resolved without a comment, so I assume that GitHub resolved it because of an unrelated change: #763 (comment)

Or am I missing something maybe?

@sebastianbenz
Copy link
Collaborator Author

Hah! Not sure how this got lost. Fixed in #811

@sebastianbenz
Copy link
Collaborator Author

Thanks for picking up on this!

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants