Skip to content

Commit 3b31dac

Browse files
mattiLeBlanccrisbeto
authored andcommitted
fix(youtube-player): invalid URL when playlist is passed in without videoId (#31403)
* Fix null issue in url youtube url when running a playlist with no videoId When playing a playlist via playerVars.list = [playlistId] we dont have a videoId available yet. Plus we don't need one, Youtube will start at the first video of the playlist by default. By injecting an undefined videoId in the YT Player constructur, we end up with null value in the iframe url (`ps://www.youtube.com/embed/null?cc_load_policy=1&enablejsapi=1&controls=1&showinfo=0&rel=0&listType=playlist&list=PLxf07yK_HAvdwhW-L0X3uP5aMe6eEt50v`) which is causing an Invalid Video Id error for consumers. * Update youtube-player.ts apply YT.PlayerOptions to params * Update youtube-player.ts fix formatting * Update youtube-player.spec.ts fix unit test * Update youtube-player.ts Fix formatting (cherry picked from commit 2c87ec3)
1 parent c3bb067 commit 3b31dac

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ describe('YoutubePlayer', () => {
515515
let calls = playerCtorSpy.calls.all();
516516

517517
expect(calls.length).toBe(1);
518-
expect(calls[0].args[1]).toEqual(jasmine.objectContaining({playerVars, videoId: undefined}));
518+
expect(calls[0].args[1]).toEqual(jasmine.objectContaining({playerVars}));
519519

520520
playerSpy.destroy.calls.reset();
521521
playerCtorSpy.calls.reset();

src/youtube-player/youtube-player.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -587,17 +587,22 @@ export class YouTubePlayer implements AfterViewInit, OnChanges, OnDestroy {
587587

588588
// Important! We need to create the Player object outside of the `NgZone`, because it kicks
589589
// off a 250ms setInterval which will continually trigger change detection if we don't.
590+
const params: YT.PlayerOptions = {
591+
host: this.disableCookies ? 'https://www.youtube-nocookie.com' : undefined,
592+
width: this.width,
593+
height: this.height,
594+
// Calling `playVideo` on load doesn't appear to actually play
595+
// the video so we need to trigger it through `playerVars` instead.
596+
playerVars: playVideo ? {...(this.playerVars || {}), autoplay: 1} : this.playerVars,
597+
};
598+
// We only want to injecct a videoId if one is provided, otherwise loading a playlist via
599+
// playerVars.list, the missing videoId will create a null value in the youtube iframe url
600+
// and that can trigger a JS error `Invalid video id` in widget api.
601+
if (this.videoId) {
602+
params.videoId = this.videoId;
603+
}
590604
const player = this._ngZone.runOutsideAngular(
591-
() =>
592-
new YT.Player(this.youtubeContainer.nativeElement, {
593-
videoId: this.videoId,
594-
host: this.disableCookies ? 'https://www.youtube-nocookie.com' : undefined,
595-
width: this.width,
596-
height: this.height,
597-
// Calling `playVideo` on load doesn't appear to actually play
598-
// the video so we need to trigger it through `playerVars` instead.
599-
playerVars: playVideo ? {...(this.playerVars || {}), autoplay: 1} : this.playerVars,
600-
}),
605+
() => new YT.Player(this.youtubeContainer.nativeElement, params),
601606
);
602607

603608
const whenReady = (event: YT.PlayerEvent) => {
@@ -710,9 +715,9 @@ export class YouTubePlayer implements AfterViewInit, OnChanges, OnDestroy {
710715
player.addEventListener(name, listener);
711716
},
712717
listener => {
713-
// The API seems to throw when we try to unbind from a destroyed player and it doesn't
714-
// expose whether the player has been destroyed so we have to wrap it in a try/catch to
715-
// prevent the entire stream from erroring out.
718+
// The API seems to throw when we try to unbind from a destroyed player and it
719+
// doesn'texpose whether the player has been destroyed so we have to wrap it in a
720+
// try/catch to prevent the entire stream from erroring out.
716721
try {
717722
player?.removeEventListener?.(name, listener);
718723
} catch {}

0 commit comments

Comments
 (0)