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

Adapt heights, sizes and media attributes for SSR #4482

Merged
merged 41 commits into from
May 8, 2020

Conversation

schlessera
Copy link
Collaborator

@schlessera schlessera commented Mar 31, 2020

Summary

This PR adds logic to the ServerSideRendering transformer in ampproject/optimizer to adapt the heights, sizes and media attributes on AMP elements and turn them into CSS. This is done so that they don't block server-side layout application.

The PR also moves the AMP_DOM_Utils::get_element_id() to AmpProject\Dom\Document::getElementId() and deprecates the previous method.

Fixes #4439

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@googlebot googlebot added the cla: yes Signed the Google CLA label Mar 31, 2020
@schlessera schlessera marked this pull request as ready for review March 31, 2020 09:47
@schlessera schlessera added this to the v1.5.1 milestone Mar 31, 2020
@westonruter westonruter modified the milestones: v1.5.1, v1.5.2, v1.6 Mar 31, 2020
@westonruter westonruter removed this from the v1.6 milestone Apr 16, 2020
@westonruter
Copy link
Member

westonruter commented Apr 18, 2020

Here's my plan for getting this merged:

  • 1. Update the AMP spec to the latest via Update allowed tags/attributes from spec in amphtml 2004142326360 #4548.
  • 2. Create a new PR which eliminates the removal of the sizes attribute in favor of adding the new disable-inline-width attribute. See Discontinue removing sizes attribute when converting img to amp-img #4606.
  • 3. Merge the develop branch into this branch to test those changes with the SSR changes. At the moment the sizes attribute is stripped from all images generated by WordPress, but this is no longer done then the changes in this PR to adapt sizes for SSR will be hugely more important. Also, I suspect that amp-img[sizes][disable-inline-width] will actually be exempt from requiring the boilerplate, so that will be a new condition to check for in the Optimizer. cc @sebastianbenz

@sebastianbenz
Copy link

Good find! Really interesting! I'm wondering whether it'd make sense to make this the default behavior for sizes (for transformed AMP) or whether there are cases where the behavior differs.

@schlessera schlessera requested a review from westonruter May 7, 2020 08:38
Comment on lines 870 to 871
'#__ID__:first-child{height:%s}',
'@media %s{#__ID__:first-child{height:%s}}'
Copy link
Member

Choose a reason for hiding this comment

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

This won't work as expected, as the :first-child is a pseudo-selector qualifying what it is attached to. So this would match elements that have ID of __ID__ which are the first-child position. So it needs rather to be:

Suggested change
'#__ID__:first-child{height:%s}',
'@media %s{#__ID__:first-child{height:%s}}'
'#__ID__>:first-child{height:%s}',
'@media %s{#__ID__>:first-child{height:%s}}'

So it selects the first child of the element that has the ID.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, you are right. Will correct.

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 just realized that it is not the height that needs to be adapted for the sizer, I think, but the padding-top.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

... which can't be set as it is inlined by JS already. 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So, as the Optimizer is the one that inlined the padding-top, I could skip that if I detect that I already had a heights attribute that turned into CSS.

But at what point are we just plain "hacking" 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.

Oh, I could actually store the CSS and properly inline it onto the sizer. That way, I can omit adding it to the size-limited <style amp-custom> as well.

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'm running in circles here. Inline styles don't allow for media queries. I will just skip adding the padding-top in that case for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, I've got a working version now. Not the most elegant stuff, but it seems to work as expected.

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'm noticing differences in the rendering of the elements in with and without SSR here.

Given this post_content:

<!-- wp:paragraph -->
<p>Heights:</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<amp-img alt="AMP" src="https://amp.dev/static/inline-examples/images/amp.jpg" width="320" height="256" heights="(min-width:500px) 200px, 80%">
</amp-img>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>Sizes:</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<amp-img alt="Hummingbird" src="https://amp.dev/static/inline-examples/images/hummingbird-wide.jpg" width="640" height="457" srcset="https://amp.dev/static/inline-examples/images/hummingbird-wide.jpg 640w,
            https://amp.dev/static/inline-examples/images/hummingbird-narrow.jpg 320w" sizes="(min-width: 650px) 50vw, 100vw">
</amp-img>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>Media:</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<amp-img media="(min-width: 650px)" src="https://amp.dev/static/inline-examples/images/hummingbird-wide.jpg" width="640" height="457" layout="responsive">
</amp-img>
<amp-img media="(max-width: 649px)" src="https://amp.dev/static/inline-examples/images/hummingbird-narrow.jpg" width="320" height="229" layout="responsive">
</amp-img>
<!-- /wp:html -->

The sizes and heights don't seem to be working right:

Width Without SSR With SSR
600px wordpressdev lndo site_2020_05_07_images-with-heights-sizes-and-media__amp amp_ssr=false (2) wordpressdev lndo site_2020_05_07_images-with-heights-sizes-and-media__amp amp_ssr=true (2)
700px wordpressdev lndo site_2020_05_07_images-with-heights-sizes-and-media__amp amp_ssr=false (3) wordpressdev lndo site_2020_05_07_images-with-heights-sizes-and-media__amp amp_ssr=true (3)

The media example is working, however.

This is using the Twenty Twenty theme.

lib/optimizer/src/Transformer/ServerSideRendering.php Outdated Show resolved Hide resolved
lib/optimizer/src/Transformer/ServerSideRendering.php Outdated Show resolved Hide resolved
@schlessera
Copy link
Collaborator Author

I'm currently investigating the visual regression with SSR. What I can already see is that the SSR is somehow changing the images from layout=responsive into layout=fixed. I suspect that something I'm doing is subsequently misinterpreted by the AMP runtime.

@schlessera
Copy link
Collaborator Author

The differences were due to the fact that the sizes, heights and media attributes were remove too early and were missing for the layout calculations, which yielded in wrong layout attributes to be assigned.

I resolved this by deferring the attribute removal via 8767882

With your post content, on my system it looks identical now. Please double-check on your end.

@schlessera schlessera requested a review from westonruter May 8, 2020 07:33
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.

It works!

@schlessera
Copy link
Collaborator Author

Based on the above issues I also created a new issue #4671, as I think we're at a point where that could drastically reduce peer review time without impacting general coding too much.

@westonruter westonruter merged commit 17f48ff into develop May 8, 2020
@westonruter westonruter deleted the add/4439-transform-media-sizes-heights branch May 8, 2020 16:35
@westonruter
Copy link
Member

Based on the above issues I also created a new issue #4671, as I think we're at a point where that could drastically reduce peer review time without impacting general coding too much.

Yes, great idea. That will be very useful.

@westonruter westonruter added this to the v1.6 milestone Jun 2, 2020
@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Jul 17, 2020
@schlessera schlessera added the WS:Perf Work stream for Metrics, Performance and Optimizer label Aug 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelogged Whether the issue/PR has been added to release notes. cla: yes Signed the Google CLA Optimizer WS:Perf Work stream for Metrics, Performance and Optimizer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement transformer in optimizer for media, sizes, and heights
4 participants