paulirish / lite-youtube-embed

A faster youtube embed.
https://paulirish.github.io/lite-youtube-embed/
Other
5.6k stars 259 forks source link

Yield on LiteYTEmbed click listener to reduce INP impact #180

Open gilbertococchi opened 2 months ago

gilbertococchi commented 2 months ago

RUM data on some project I have suggested that "lite-youtube.lyt-activated>button.lty-playbtn" interaction is causing large INP.

I would suggest yielding on the Click Event Handler doing something similar to this and improve the UX somehow like changing the Playback icon status etc to improve UX on top of INP.

this.addEventListener('click', function(event) {
            event.preventDefault();
            requestAnimationFrame(() => {
                setTimeout(() => {
                  if (this.classList.contains('lyt-activated')) return;
                  ...
gilbertococchi commented 2 months ago

Before Code Change (Note that code is happening in the YT Iframe Thread)

before

After Code Change

after
paulirish commented 2 months ago

Hmm.. Those look like other folks' event listeners.

Here's what that initial click on the play button looks like to me.. just a 20ms interaction:

image

gilbertococchi commented 2 months ago

I have RUM data that suggests it's caused by YT iFrame main thread code triggered by the play-btn click event with target attribution, no other event handlers.

It's the YouTube Iframe Thread code that increased the play-btn Click Event Handler blocking time.

It's true that my lite-youtube-embed fork acts differently, loading the YT Iframe in the background, so it's much faster to start playback.

I should have fixed it by adding the yield code to the Click event listener as described, will update you once I get new daily data.

Screenshot 2024-05-06 at 21 22 59