Lazy load embed bento components to improve performance
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.
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, 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:
- Do we want to enable this functionality for both Bento Web Components and (P)react mode, or just WC?
- Should the viewport distance be customizable? How will it interact with the AMP-side scheduling (i.e. maybe disable for that mode).
- How should viewport distance detection be implemented? Right now I see a
.getBoundingClientRectin 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)
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.
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.