Skip to content

Commit b0d0388

Browse files
authored
fix(youtube-player): YT.Player is not a constructor (#20616)
* fix(youtube-player): YT.Player is not a constructor fixes a race condition where window.YT would be truthy but window.YT.Player would not be ready. Fixes #20598 * fix(youtube-player): YT.Player is not a constructor test added a test to prevent reggression, the test mimics a possible state of the YT object when loading, when window.YT exists but window.YT.Player does not. Fixes #20598
1 parent 5c42f3c commit b0d0388

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/youtube-player/youtube-player.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {createFakeYtNamespace} from './fake-youtube-player';
66
import {Subscription} from 'rxjs';
77

88
const VIDEO_ID = 'a12345';
9+
const YT_LOADING_STATE_MOCK = {loading: 1, loaded: 0};
910

1011
describe('YoutubePlayer', () => {
1112
let playerCtorSpy: jasmine.Spy;
@@ -381,6 +382,8 @@ describe('YoutubePlayer', () => {
381382
});
382383

383384
it('waits until the api is ready before initializing', () => {
385+
(window.YT as any) = YT_LOADING_STATE_MOCK;
386+
384387
fixture = TestBed.createComponent(TestApp);
385388
testComponent = fixture.debugElement.componentInstance;
386389
fixture.detectChanges();

src/youtube-player/youtube-player.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
210210
}
211211

212212
let iframeApiAvailableObs: Observable<boolean> = observableOf(true);
213-
if (!window.YT) {
213+
if (!window.YT || !window.YT.Player) {
214214
if (this.showBeforeIframeApiLoads && (typeof ngDevMode === 'undefined' || ngDevMode)) {
215215
throw new Error('Namespace YT not found, cannot construct embedded youtube player. ' +
216216
'Please install the YouTube Player API Reference for iframe Embeds: ' +

0 commit comments

Comments
 (0)