Skip to content

Fix null issue in url youtube url when running a playlist with no vid… #31403

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

Merged
merged 5 commits into from
Jun 25, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/youtube-player/youtube-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,17 +587,21 @@ export class YouTubePlayer implements AfterViewInit, OnChanges, OnDestroy {

// Important! We need to create the Player object outside of the `NgZone`, because it kicks
// off a 250ms setInterval which will continually trigger change detection if we don't.
const params: any = {
Copy link
Member

Choose a reason for hiding this comment

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

This can be typed as YT.PlayerOptions, rather than any.

host: this.disableCookies ? 'https://www.youtube-nocookie.com' : undefined,
width: this.width,
height: this.height,
// Calling `playVideo` on load doesn't appear to actually play
// the video so we need to trigger it through `playerVars` instead.
playerVars: playVideo ? {...(this.playerVars || {}), autoplay: 1} : this.playerVars,
}
// We only want to injecct a videoId if one is provided, otherwise loading a playlist via playerVars.list, the missing videoId will create a null value in the youtube iframe url and that can trigger a JS error `Invalid video id` in widget api.
if (this.videoId) {
params.videoId = this.videoId;
}
const player = this._ngZone.runOutsideAngular(
() =>
new YT.Player(this.youtubeContainer.nativeElement, {
videoId: this.videoId,
host: this.disableCookies ? 'https://www.youtube-nocookie.com' : undefined,
width: this.width,
height: this.height,
// Calling `playVideo` on load doesn't appear to actually play
// the video so we need to trigger it through `playerVars` instead.
playerVars: playVideo ? {...(this.playerVars || {}), autoplay: 1} : this.playerVars,
}),
new YT.Player(this.youtubeContainer.nativeElement, params),
);

const whenReady = (event: YT.PlayerEvent) => {
Expand Down
Loading