videojs / videojs-youtube

YouTube playback technology for Video.js
1.13k stars 547 forks source link

Lazily load the YouTube API #586

Open boris-petrov opened 3 years ago

boris-petrov commented 3 years ago

YouTube's API is loaded here once videojs-youtube is loaded. Is it possible this to be done lazily only when a video from YouTube is requested? Otherwise just importing this plugin loads a bunch of code that could go unused if no YouTube video is loaded. Not sure if that is possible in VideoJs' API though?

gkatsev commented 3 years ago

It might be possible but could potentially lead to other issues like slow response to the user. The worst case scenario I can see, though, unlikely, is that if things are loaded on demand, a user hits play and youtube loads but is unable to play the video because of autoplay restrictions because it's been too long from when the user hit the play button. Only thing I can think of of providing this option is by requiring videojs.options.youtube be set before the youtube script loads. For example:

<script src="video.js"></script>
<script>
  // defaults to off to keep current behavior
  videojs.options.youtube = {
    lazyLoadScript: true
  };
</script>
<script src="youtube.js"></script>

Then, we can check for lazyLoadScript where you posted https://github.com/videojs/videojs-youtube/blob/v2.6.1/src/Youtube.js#L788 and also do a check in the tech's constructor to load the script if it hasn't already. May need to update other pieces of the tech to handle the late loading of the script.

Would you be interested in making a PR for this? Unfortunately, it's unlikely that we'll have the time to implement this feature.

boris-petrov commented 3 years ago

@gkatsev - thanks for taking the time to give a detailed explanation of what should be done. I don't believe that I'll have time (for now) to look into this as well. Let's leave this open and some day I or someone else might step up to do it. :) Thanks!