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

Use fixed-height layout for amp-tiktok and add height estimation #6586

Merged
merged 2 commits into from
Aug 31, 2021

Conversation

westonruter
Copy link
Member

Summary

When testing #6060 I found that there was quite a bit of layout shifting going on, even beyond what was reported in ampproject/amphtml#35789. The issue is with the responsive layout because as can be seen below, even when amp-tiktok has a responsive layout, the element does not resize responsively. Rather, the element has a fixed-height layout:

fixed-height.mov

The problem with the responsive layout is that the elements amp-tiktok elements may start out extremely tall on wide pages before the elements initialize and their actual heights are supplied. This can result in a lot of layout shift, although not as bad as the non-AMP version still:

before.mov

Therefore, this PR changes the layout of the amp-tiktok elements from responsive to fixed-height. Additionally, it estimates the height of the metadata card by looking at the length of the description provided in the oEmbed response. The result is much improvement to CLS even to be considered good when there are 4 instances of amp-tiktok on the page, with a vast improvement over the non-AMP oEmbed as provided by TikTok:

after.mov

The post_content being shown here is as follows:

<!-- wp:html -->
<p id="cls" style="padding:10px; font-size: 30px; background-color:lime;">CLS: <output style="font-weight:bold">0</output></p>
<script data-ampdevmode="">
let clsValue = 0;
new PerformanceObserver((entryList) => {
	for (const entry of entryList.getEntries()) {
		if (!entry.hadRecentInput) {
			clsValue += entry.value;
			const clsElement = document.getElementById('cls');
			clsElement.querySelector('output').textContent = clsValue;
			if ( clsValue >= 0.25 ) {
				clsElement.style.backgroundColor = 'red';
			} else if ( clsValue > 0.1 ) {
				clsElement.style.backgroundColor = 'orange';
			}
		}
	}
}).observe({type: 'layout-shift', buffered: true});
</script>
<!-- /wp:html -->

<!-- wp:embed {"url":"https://www.tiktok.com/@scout2015/video/6718335390845095173","type":"video","providerNameSlug":"tiktok","responsive":true,"align":"wide"} -->
<figure class="wp-block-embed alignwide is-type-video is-provider-tiktok wp-block-embed-tiktok"><div class="wp-block-embed__wrapper">
https://www.tiktok.com/@scout2015/video/6718335390845095173
</div></figure>
<!-- /wp:embed -->

<!-- wp:embed {"url":"https://www.tiktok.com/@deeptomcruise/video/6932166297996233989?lang=en\u0026is_copy_url=0\u0026is_from_webapp=v1\u0026sender_device=pc\u0026sender_web_id=6969641973003388422","type":"video","providerNameSlug":"tiktok","responsive":true,"align":"wide"} -->
<figure class="wp-block-embed alignwide is-type-video is-provider-tiktok wp-block-embed-tiktok"><div class="wp-block-embed__wrapper">
https://www.tiktok.com/@deeptomcruise/video/6932166297996233989?lang=en&amp;is_copy_url=0&amp;is_from_webapp=v1&amp;sender_device=pc&amp;sender_web_id=6969641973003388422
</div></figure>
<!-- /wp:embed -->

<!-- wp:embed {"url":"https://www.tiktok.com/@scooterforlifee/video/6988674492520287493?sender_device=pc\u0026sender_web_id=6969641973003388422\u0026is_from_webapp=v1\u0026is_copy_url=0","type":"video","providerNameSlug":"tiktok","responsive":true,"align":"wide"} -->
<figure class="wp-block-embed alignwide is-type-video is-provider-tiktok wp-block-embed-tiktok"><div class="wp-block-embed__wrapper">
https://www.tiktok.com/@scooterforlifee/video/6988674492520287493?sender_device=pc&amp;sender_web_id=6969641973003388422&amp;is_from_webapp=v1&amp;is_copy_url=0
</div></figure>
<!-- /wp:embed -->

<!-- wp:embed {"url":"https://www.tiktok.com/@gordonramsayofficial/video/6986343426744798470?lang=en\u0026is_copy_url=1\u0026is_from_webapp=v1","type":"video","providerNameSlug":"tiktok","responsive":true,"align":"wide"} -->
<figure class="wp-block-embed alignwide is-type-video is-provider-tiktok wp-block-embed-tiktok"><div class="wp-block-embed__wrapper">
https://www.tiktok.com/@gordonramsayofficial/video/6986343426744798470?lang=en&amp;is_copy_url=1&amp;is_from_webapp=v1
</div></figure>
<!-- /wp:embed -->

Checklist

  • 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).

@github-actions
Copy link
Contributor

github-actions bot commented Aug 31, 2021

Plugin builds for 1638488 are ready 🛎️!

Co-authored-by: Pierre Gordon <16200219+pierlon@users.noreply.github.com>
Copy link
Contributor

@pierlon pierlon left a comment

Choose a reason for hiding this comment

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

LGTM!

Comment on lines +67 to +81
// Initial height of video (most of them anyway).
$height = 575;

// Add the height of the metadata card with the CTA, username, and audio source.
$height += 118;

// Estimate the lines of text in the paragraph description (150-character limit).
$p = $blockquote->getElementsByTagName( Tag::P )->item( 0 );
if ( $p instanceof Element ) {
$height += 8; // Top margin.

// Add height for the lines of text, where there are approx. 39 chars fit on
// a line, and a line's height is 18px.
$height += ceil( strlen( trim( $p->textContent ) ) / 39 ) * 18;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Quite a clever way of determining the height!

@westonruter westonruter merged commit ce8bea5 into develop Aug 31, 2021
@westonruter westonruter deleted the fix/amp-tiktok-height branch August 31, 2021 05:49
westonruter added a commit that referenced this pull request Aug 31, 2021
Co-authored-by: Pierre Gordon <pierregordon@protonmail.com>
@pierlon pierlon self-assigned this Aug 31, 2021
@pierlon
Copy link
Contributor

pierlon commented Aug 31, 2021

QA Passed

Given a post with the following content:

<!-- wp:embed {"url":"https://www.tiktok.com/@scout2015/video/6718335390845095173","type":"video","providerNameSlug":"tiktok","responsive":true,"align":"wide"} -->
<figure class="wp-block-embed alignwide is-type-video is-provider-tiktok wp-block-embed-tiktok"><div class="wp-block-embed__wrapper">
https://www.tiktok.com/@scout2015/video/6718335390845095173
</div></figure>
<!-- /wp:embed -->

<!-- wp:embed {"url":"https://www.tiktok.com/@deeptomcruise/video/6932166297996233989?lang=en\u0026is_copy_url=0\u0026is_from_webapp=v1\u0026sender_device=pc\u0026sender_web_id=6969641973003388422","type":"video","providerNameSlug":"tiktok","responsive":true,"align":"wide"} -->
<figure class="wp-block-embed alignwide is-type-video is-provider-tiktok wp-block-embed-tiktok"><div class="wp-block-embed__wrapper">
https://www.tiktok.com/@deeptomcruise/video/6932166297996233989?lang=en&amp;is_copy_url=0&amp;is_from_webapp=v1&amp;sender_device=pc&amp;sender_web_id=6969641973003388422
</div></figure>
<!-- /wp:embed -->

It can be observed that the amp-tiktok embeds are now using a fixed-height layout with a variable height:

  • Embed 1

image

  • Embed 2

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants