amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Lazy load embed bento components to improve performance

Open deepaklalwani97 opened this issue 4 years ago • 3 comments

Description

All the bento components with embeds load all the required scripts for embeds on the page load which affects the performance of the page and reduces page insights score.

Screenshot 2021-11-01 at 6 20 31 PM

This can be avoided by lazy loading the embed components such as Twitter, Youtube, Soundcloud. Etc using the first user scroll action or loading component when it comes in the viewport using the intersection observer when the embed is below the fold. This will prevent the third-party embed scripts from blocking the main thread on page load.

Reproduction Steps

  • Open https://bento-twitter.glitch.me/
  • The twitter.js and widget.js can be seen loaded on the page load in the dev console and it will block the main thread.

Relevant Logs

No response

Browser(s) Affected

No response

OS(s) Affected

No response

Device(s) Affected

No response

AMP Version Affected

No response

deepaklalwani97 avatar Dec 09 '21 07:12 deepaklalwani97

@deepaklalwani97, thank you for raising this! It is a great idea.

On the AMP side, we actually already have this concept via the resources scheduling system. If you create a similar document with amp-twitter you should see the only network activity is the preconnect, and the bulk of the JS for the embed doesn't run until within 3 viewports away.

It could definitely be a good idea to create something similar for Bento. There are a lot of nuances here with how we implement it, and I think it deserves a greater design discussion along the lines of:

  1. Do we want to enable this functionality for both Bento Web Components and (P)react mode, or just WC?
  2. Should the viewport distance be customizable? How will it interact with the AMP-side scheduling (i.e. maybe disable for that mode).
  3. How should viewport distance detection be implemented? Right now I see a .getBoundingClientRect in the linked draft implementation, but we should likely take advantage of a shared hook, like useIntersectionObserver

Finally, we should also ensure our solution can be easily adopted by other heavy components (most likely just embeds)

samouri avatar Dec 15 '21 21:12 samouri

Thanks, @samouri Sure we will take a deeper look at the AMP resource scheduling system and see how we can create something similar for the Bento and propose it in the design review meeting with above points.

deepaklalwani97 avatar Dec 28 '21 11:12 deepaklalwani97

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 07 '23 18:01 stale[bot]