Skip to content

feat(youtube-player): add config option to automatically load the YouTube API #21401

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

Closed
wants to merge 1 commit into from

Conversation

crisbeto
Copy link
Member

Adds a new injection token that allows the YouTubePlayer to automatically load the API, instead of depending on it to be loaded by the consumer.

Fixes #17037.

@crisbeto crisbeto added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent merge safe target: minor This PR is targeted for the next minor release labels Dec 20, 2020
@google-cla google-cla bot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Dec 20, 2020

@NgModule({
imports: [YouTubePlayerModule],
// Optionally tells the `youtube-player` component to automatically load
// the YouTube iframe API. Omit this if you plan to load the API yourself.
providers: [{
Copy link
Member Author

@crisbeto crisbeto Dec 20, 2020

Choose a reason for hiding this comment

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

@jelbourn I would appreciate your feedback on a couple of things that I was considering when implementing it:

  1. How the config should be provided: through an injection token like here or through something like YouTubePlayerModule.withConfig({ loadApi: true }). I went with the former, because it's clearer what is going on, but I can see an argument for the latter, because the API is a bit cleaner and I expect that the most common use case would be to let the component load the API automatically. I wouldn't mind changing it to use the second approach.
  2. Whether loadApi should be turned on by default. I decided against it, because it does add an external script to people's apps and I figured that's something we'd want them to opt into, but it does make it slightly harder to consume.

@jelbourn
Copy link
Member

jelbourn commented Jan 5, 2021

I'm still quite leery of adding an API that loads the API for you. My rationale is along the lines of:

  • This isn't something we do for any other dependency.
  • I generally think developers should be aware of how they're loading deps into their application, and I'm not a fan of having a black-box API that obscures that.
  • Having an API for loading the dependency implies that there's a "correct" way to do it, when it depends somewhat on what the app wants to do.
  • It's easier for apps to end up in a state with multiple versions of the dependency loaded

I'll also comment this on the issue

@crisbeto
Copy link
Member Author

crisbeto commented Jan 6, 2021

I think that your points are valid, but here's why I decided to do it:

  1. Getting the loading "right" can be tricky, because you'd have to coordinate it between inserting the script with the DOM API and keeping the component from initializing through something like ngIf. It's much easier to do it once on our end.
  2. If you want to load the API yourself only when the component is used, you'll either have repeat some logic or write a wrapper component which means that you'd have to forward all the inputs.
  3. The only way to consume the API is through a script pointing to the Google CDN. As far as I'm aware, there's no option to host it yourself, unless you copy the script somewhere, but then it can break on you at any time.

Regarding your points:

This isn't something we do for any other dependency.

I was planning to do something similar for the Google Maps API, if we managed to agree how this one would be handled, since it's set up in the exact same way.

I generally think developers should be aware of how they're loading deps into their application, and I'm not a fan of having a black-box API that obscures that.

I think that's fair for something like Angular or jQuery where there are different versions and ways to load the code. In the YouTube case, this is the only way of doing it since our component is tightly coupled to the iframe API.

Having an API for loading the dependency implies that there's a "correct" way to do it, when it depends somewhat on what the app wants to do.

As I mentioned above, in this particular case there is only one way of loading the API. The only decision is between loading it up-front or doing so lazily and I'd argue that doing it lazily is preferable.

It's easier for apps to end up in a state with multiple versions of the dependency loaded

As far as I'm aware, there is only one possible version. There's a risk of including the script tag multiple times, but our code should account for that.

…Tube API

Adds a new injection token that allows the `YouTubePlayer` to automatically load the
API, instead of depending on it to be loaded by the consumer.

Fixes angular#17037.
@crisbeto crisbeto force-pushed the youtube-player-api branch from f63a644 to e31d3c7 Compare August 2, 2021 19:31
@crisbeto crisbeto requested a review from a team as a code owner August 2, 2021 19:31
@josephperrott josephperrott removed the request for review from a team August 10, 2021 18:38
@devversion devversion removed their request for review August 18, 2021 13:06
@andrewseguin andrewseguin removed the cla: yes PR author has agreed to Google's Contributor License Agreement label Dec 28, 2021
@andrewseguin andrewseguin removed the P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent label Mar 28, 2022
@crisbeto
Copy link
Member Author

Closing in favor of #28171.

@crisbeto crisbeto closed this Nov 22, 2023
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Dec 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FR: YouTube / Google Maps should load the API itself
6 participants