Description
This is an intermittent exception due to a race condition.
The problem is in youtube-player.ts line 213. It checks if (!window.YT)
but window.YT exists before window.YT.Player exists. There is an intermediate loading state where YT.loaded = 0. I believe a solution could be to change this if to if (!window.YT || !window.TY.loaded)
or alternatively if (!window.YT || !window.TY.Player)
.
Reproduction
This Stackblitz has similar code to our app where the problem is happening, however it did not reproduce the race condition for me. https://stackblitz.com/edit/components-issue-dkswjg?file=src%2Findex.html
Steps to reproduce:
- Get the youtube iframe API by adding this to index.html: <script src="https://www.youtube.com/iframe_api"></script>
- Use the in a component that loads fairly immediately.
Expected Behavior
The player shows the video.
Actual Behavior
The player did not load. An error is logged to the console.
ERROR TypeError: YT.Player is not a constructor
at youtube-player.js:529
at ZoneDelegate.invoke (zone-evergreen.js:364)
at Zone.run (zone-evergreen.js:123)
at NgZone.runOutsideAngular (core.js:27364)
at ScanSubscriber.syncPlayerState [as accumulator] (youtube-player.js:529)
at ScanSubscriber._tryNext (scan.js:49)
at ScanSubscriber._next (scan.js:42)
at ScanSubscriber.next (Subscriber.js:49)
at MapSubscriber._next (map.js:35)
at MapSubscriber.next (Subscriber.js:49)
Environment
- Angular: 10.0.6
- CDK/Material: 10.2.1
- Browser(s): Chrome
- Operating System (e.g. Windows, macOS, Ubuntu): macOS
Possibly some commenters on #19871 were seeing this same problem.