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 SSR support for fluid layout #1232

Merged
merged 6 commits into from
Jun 7, 2021
Merged

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented May 4, 2021

The use of the fluid layout currently blocks server-side rendering since it is not among the SUPPORTED_LAYOUTS:

const SUPPORTED_LAYOUTS = [
'',
'nodisplay',
'fixed',
'fixed-height',
'responsive',
'container',
'fill',
'flex-item',
'intrinsic',
];

The fluid layout is not ideal because it inherently involves a layout shift since its dimensions get determined at runtime, and it will not get rendered when it is initially in the viewport (per docs). Nevertheless, when this layout is required it would be preferable for it to not prevent other parts of the page from benefiting from SSR.

Implementation provided by @jridgewell.

Merging this PR is blocked by the AMP validator being updated, specifically to modify validateSsrLayout() as follows:

      const validInternalClasses = Object.create(null);
      validInternalClasses[getLayoutClass(layout)] = 0;
      if (isLayoutSizeDefined(layout)) {
        // i-amphtml-layout-size-defined
        validInternalClasses[getLayoutSizeDefinedClass()] = 0;
      }
+     if ('fluid' === layout) {
+       validInternalClasses['i-amphtml-layout-awaiting-size'] = 0;
+     }
  • Add tests.
  • Verify validator updated and deployed.

cf. b/184254585

@@ -79,6 +80,13 @@ function apply(layout, width, height, node) {
case 'container':
// Do nothing here.
break;
case 'fluid':
if (width.isSet) {
styles = `width:${width.numeral}${width.unit};`;
Copy link
Contributor

Choose a reason for hiding this comment

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

When looking into it, it seems width isn't actually used or respected by fluid ads. Instead, there's a width: 100% !important set by https://github.com/ampproject/amphtml/blob/0320666d03db37c4e0e363b55f828aa807ae321c/extensions/amp-ad/0.1/amp-ad.css#L76-L78

Copy link
Member Author

Choose a reason for hiding this comment

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

The Fluid docs say:

It will always occupy the maximum available width, and its height will be determined relative to that width.

But also:

Note also that the width attribute is optional, and can be specified. When specified, the fluid creative will always occupy that width (unless used in conjunction with multi-size).

So it seem there is still a case where the width would be used?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't believe it can be, due to the !important override?

Copy link
Member Author

Choose a reason for hiding this comment

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

Since the selector is for amp-ad[data-a4a-upgrade-type='amp-ad-network-doubleclick-impl'][height='fluid'], is the data-a4a-upgrade-type="amp-ad-network-doubleclick-impl" attribute always added at runtime?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup, doubleclick is the only thing that supports fluid, and that attr gets injected when amp-ad loads.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed in d232ad2

@@ -79,6 +80,10 @@ function apply(layout, width, height, node) {
case 'container':
// Do nothing here.
break;
case 'fluid':
styles += 'height:0;';
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Added in d9876e2, but 0 seems better than 0px

westonruter added a commit to ampproject/amp-toolbox-php that referenced this pull request Jun 4, 2021
@westonruter westonruter force-pushed the add/fluid-layout-ssr-support branch from 8581b15 to 200a100 Compare June 4, 2021 21:31
@westonruter westonruter marked this pull request as ready for review June 4, 2021 21:34
schlessera pushed a commit to ampproject/amp-toolbox-php that referenced this pull request Jun 7, 2021
Copy link
Collaborator

@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.

Thanks a lot!

@sebastianbenz sebastianbenz merged commit 5ab6c02 into main Jun 7, 2021
@sebastianbenz sebastianbenz deleted the add/fluid-layout-ssr-support branch June 7, 2021 13:42
sebastianbenz pushed a commit that referenced this pull request Jun 8, 2021
* Add SSR support for fluid layout

* Remove unnecessary `width` when applying `fluid` layout

* Add `width` to applied fluid layout

* Remove needless unit from zero fluid height

* Add transforms_layout_fluid test

* Remove needless string concatenation

Co-authored-by: Alain Schlesser <[email protected]>

Co-authored-by: Alain Schlesser <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants