Skip to content

Commit

Permalink
more specificity on the autoplay useragent checks
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Sep 11, 2022
1 parent 1fa89dd commit 514598c
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/lite-yt-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,12 @@ class LiteYTEmbed extends HTMLElement {
// We'd want to only do this for in-viewport or near-viewport ones: https://github.com/ampproject/amphtml/pull/5003
this.addEventListener('click', this.addIframe);


// Chrome & Edge have no problem with the basic YouTube Embed with ?autoplay=1
// However Safari and Firefox do not successfully track the user gesture of clicking through the creation/loading of the iframe,
// so they don't autoplay automatically. Instead we must load an additional 300KB (ungz) of JS for the YT Player API
// TODO: chrome android seems to also need this
this.needsYTApiForAutoplay = navigator.vendor.includes('Apple') || ['Firefox', 'Android'].some(userAgent => navigator.userAgent.includes(userAgent));
// Chrome & Edge desktop have no problem with the basic YouTube Embed with ?autoplay=1
// However Safari desktop and most/all mobile browsers do not successfully track the user gesture of clicking through the creation/loading of the iframe,
// so they don't autoplay automatically. Instead we must load an additional 2 sequential JS files (1KB + 165KB) (un-br) for the YT Player API
this.needsYTApiForAutoplay = navigator.vendor.includes('Apple') || navigator.userAgent.includes('Mobi');
}

// // TODO: Support the the user changing the [videoid] attribute
// attributeChangedCallback() {
// }

/**
* Add a <link rel={preload | preconnect} ...> to the head
*/
Expand Down

0 comments on commit 514598c

Please sign in to comment.