July 7th, 2024

YouTube Embeds Are Bananas Heavy and It's Fixable

YouTube embeds are criticized for their heavy size, impacting website performance. Web Components offer a lighter alternative, like lite-youtube-embed, maintaining functionality while reducing size to 100k. This shift enhances efficiency and user experience.

Read original articleLink Icon
YouTube Embeds Are Bananas Heavy and It's Fixable

YouTube embeds are highlighted as being excessively heavy, with each embed weighing around 1.3MB and lacking shared resources between multiple embeds. This issue can impact website performance significantly. The article suggests using Web Components as a more efficient alternative, reducing the size to about 100k and maintaining functionality. The current YouTube embeds include a poster image, video title, and a play button, but the heavy resource usage raises concerns about environmental impact and efficiency. The solution proposed is to replicate the embed experience using open-source Web Components like lite-youtube-embed, which offers a faster and more efficient way to display YouTube videos without sacrificing functionality. By implementing these lightweight alternatives, websites can improve performance, reduce data transfer, and enhance user experience. Various resources and alternatives are provided for developers interested in optimizing video embeds on their websites.

Link Icon 40 comments
By @charrondev - 3 months
I work on a community forum platform and we detect YouTube embeds and replace them proxied thumbnails that don’t load until clicked.

Just because one person in a thread shares a YouTube video doesn’t mean everyone else loading that page should have to download 1MB+ of YouTube’s JavaScript bloat and have their IPs tracked by google.

By @mmmmmbop - 3 months
The author says they don't believe that a lighter version has been shown to reduce engagement.

I, on the other hand, fully believe that.

The recommended lite-youtube-embed project page has a demo of both lite and regular players [0], and the lite version takes noticeably longer to start playing the video.

Every additional millisecond of load time will reduce engagement, and here the difference is more on the order of hundreds of milliseconds or more.

[0] https://paulirish.github.io/lite-youtube-embed/

By @qwery - 3 months
On not believing the reduced engagement rumour and the suggested 'lite-youtube-embed'[0]:

I am not surprised to hear that a different player will be treated differently by users. You just need it to look slightly different or behave slightly differently and it's completely alien and not to be trusted.

The lite-youtube-embed as demoed (even with the title visible) doesn't let me click through to the actual youtube page. There's no link. It could even appear as a no-right-click-esque attempt to prevent me from going to the actual source of the "content" -- it's hostile. Of course, this specific feature could be added easily enough, but it points out a bigger issue.

I almost never play embedded video, but when I do it might as well be the normal youtube experience. If you've wrapped it in what looks like yet another layer (for an unknown purpose), I'm going to be less likely to click on it. You're asking me to contend with youtube/google and another unknown entity.

This article's "nothing sacrificed" is an example of the mistaken belief that developers know how their (or other) software is used by users. You don't and you can't. Not really. You're always guessing.

To be clear, I also want less videos everywhere, less google in everything, less megabytes of javascript in everything, etc. Please stop embedding youtube videos everywhere, to vote for a better web.

[0] https://paulirish.github.io/lite-youtube-embed/

By @ethanol - 3 months
Now we only need to force bloggers to stop using GitHub Gist embeds. Hugo (and probably other static site generators) has built-in support for code snippets with syntax highlighting, and more dynamic sites can rely on highlight.js which removes dependence on third-party services. It's just insane, using heavy iframes for small code snippets.

https://gohugo.io/content-management/syntax-highlighting/

https://highlightjs.org

By @donohoe - 3 months
One way to help reduce overall weight of embeds (and improve the UX imho) is to block the ads - if you are able to leverage "Content Security Polices" on your pages.

Example META tag:

  <meta http-equiv="Content-Security-Policy" 
  content="default-src 'self' 'unsafe-inline' *.your-cdn-if-any.com 
  www.youtube.com *.googlevideo.com *.ytimg.com">
More info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Co...
By @Aurornis - 3 months
> I don’t think we have any good answers here. In fact, I heard from a little birdie who ran it up the pole that they have tested lighter embeds and found them to reduce engagement.

We’re clearly missing some huge part of the story.

Obviously, faster load times should improve engagement. So if engagement went down with lighter embeds, that implies that some feature or functionality had to be sacrificed. Yet this blog post claims nothing is sacrificed.

Something is missing from this story.

By @user070223 - 3 months
User side solution click 2 load for ublock users(note that chrome is transitioning to manifest v3 and might not work) is the following thanks to yokoffing/filterlists https://raw.githubusercontent.com/yokoffing/filterlists/main... (Betterfox creator, he has other useful filters on github)

  !||youtube-nocookie.com^$3p,frame,redirect=click2load.html,domain=~bing.com|~google.com|~duckduckgo.com|~video.search.yahoo.com
  ||youtube-nocookie.com/embed^$3p,frame,redirect=click2load.html,domain=~bing.com|~google.com|~duckduckgo.com|~video.search.yahoo.com
  ||youtube.com^$3p,frame,redirect=click2load.html,domain=~bing.com|~chatreplay.stream|~google.com|~duckduckgo.com|~video.search.yahoo.com
By @divbzero - 3 months
> The weight also grows linearly with every embed—resources are not shared: two embeds weigh 2.4 MB; three embeds weigh 3.6 MB (you get the idea).

Why aren’t these resources retrieved from cache? Shouldn’t the same-origin policy should allow for use of cached resources since they are all loading from www.youtube.com?

By @dangus - 3 months
My favorite part of the article:

> They found, quite counterintuitively, that average page load times went up. But with a deeper look, they found that the lighter page was able to reach more people, including people on low-power low-internet-speed devices who were able to actually use YouTube for the first time, and them using it much more slowed those averages. That’s awesome! The speed of using the site was up relatively for everyone. The metric of the average page load speed was a red herring and ultimately not meaningful.

By @kevincox - 3 months
Back in the day I had a Firefox extension that would replace all YouTube embeds with a thumbnail that when clicked would just open YouTube.

I originally wrote it because my phone didn't have flash and flash embeds were common. But it was nice to extend it to cover iframe embeds as well which were much faster and opening the embed in a full YouTube website or app was better UX than a tiny embed window in most cases.

I never migrated it to a WebExtension after being annoyed that I wrote it to the new high-level extension API at the time that was supposed to be supported across browser versions. But this post makes me want to port it.

By @lulzury - 3 months
If you have a small site or just don't want any requests to Google for privacy reasons, consider just straight-up downloading the video and embedding it in a video tag. There are yt_dlp wrappers for most popular languages (even js).

I'm pretty sure even embedding their poster thumbnail results in them getting your IP and other information so consider downloading that as well (from https://img.youtube.com/vi/[TAG]/hqdefault.jpg).

By @GuB-42 - 3 months
[Ignore this, everything is ok now]

For me the website is slow as to border on unusable. I have one core at 100% on Firefox 115 / Debian 11, I guess there is a busy loop somewhere in the JS.

Works fine on Chrome(ium).

I know it is usually frowned upon to comment on the website itself rather than the content, but considering the nature of the website, I think it is relevant.

Edit: Looked in a bit more detail and it looks CSS-related, not JS-related as removing the main style sheet fixes the problem. It happens even in safe mode (no extensions). Possibly a Firefox bug (version is 115.12.0esr, from the Debian 11 repository), but it doesn't happen anywhere else.

Edit2: Updated my system, rebooted, etc... it fixed the problem. So either I had something messed up with my system, or the author fixed it, anyway, everything is ok now.

By @justsomehnguy - 3 months
> The Solution is to Replicate the Embed Experience Another Way.

No, The Solution is to publicly shame Google Inc for wasting the resources.

When it would be on WSJ and The Guardian front page then, maybe, you would see the result.

Replacing the embed a one server/page is like pissing in the ocean - feels great, does nothing.

By @donatj - 3 months
I am not understanding how Bananas play into this?

Is that a term for curly braces I am unfamiliar with or something with? Is code being "banana heavy" a term for code having a lot of scopes thus being bloated? I'm grasping at straws here.

By @userbinator - 3 months
Embedded videos could just be a <video> element pointing to the file on YouTube's servers (and I believe for a short time long ago this was actually possible), but clearly commercial interests got in the way.
By @015a - 3 months
> If I were Team YouTube, I’d get loading="lazy" on there to help with performance right away. No need for videos that aren’t even visible on the page to load right away.

This would hurt advertising impressions.

By @knallfrosch - 3 months
The page is broken on ios/safari with text size not set to 100%

I don't usually care, but when you hand out advice to other sites and call your little text blog "Frontend-Masters", it better work.

By @fallingsquirrel - 3 months

  <img src="thumbnail.png" onclick="[replace image with embed]">
By @II2II - 3 months
Tangent: I find tremendous irony in the video used as an example. It is for the remake of a game that is about problem solving and story telling, yet the remake requires vastly more resources than the original. Yes, there are reasons for the increased resource usage. On the other hand, there are reasons to use the more resource intensive version of the YouTube embed.

I suppose the moral of the story is that software will grow to consume the resources available to it. Often, if not most of the time, there will be benefits to that increased resource usage. Yet that won't prevent people who prioritize factors like efficient resource usage from seeking alternatives they view as better.

By @jeffbee - 3 months
When I navigate to the test page[1] by the author of lite-embed, the standard variant transfers 82k. According to browser stats, the largest element "base.js" has been cached for several days. I imagine that the authors of YouTube who are undoubtedly as sensitive to the costs of transferring bytes as anyone have analyzed the problem with browser caches taken into account.

https://paulirish.github.io/lite-youtube-embed/variants/yt.h...

By @ghostwords - 3 months
(I develop Privacy Badger.)

Privacy Badger can replace YouTube embeds with "click to activate" placeholders. Faster browsing, better privacy, easy-to-use controls (when the replacement works properly ...).

See "Pro tip #1" in https://www.eff.org/deeplinks/2024/01/privacy-badger-puts-yo...

By @leptons - 3 months
I spent a year creating a web template system that scores 100/100/100/100 on Google's Lighthouse test, with all kinds of dynamic content. Video backgrounds and embeds were a huge problem for us, but it can be solved.

It's really pretty simple. Any video embed must be "below the fold", and the iframe must be lazy-loaded. Now our pages don't see any page speed hit from video embeds, they only get loaded when the page is scrolled and the video is in view. Any other solution that isn't lazy-loaded will still impact page speed, and if that's important to the client, then don't load anything that isn't absolutely necessary to display what appears "above the fold".

For video backgrounds, our pages make a call to one of our APIs that gets the media URL from Youtube or Vimeo, and we then play that in a standard HTML 5 video player. It works great, and our Google Lighthouse score is above 95 even on pages with above-the-fold HD video that auto-plays. Loading a JS library from anywhere just to play a video is going to hurt page speed a bit no matter what.

By @Havoc - 3 months
I'm seriously considering just setting up a service that downloads everything in my preferred channels overnight and re-serves it locally. It'll increase their cost and decrease their revenue but frankly I'm just getting fed up with their UX. It's just sooo fuckin bad I do wonder whether YT staff actually uses it.

e.g. lately the thumbnails have a popup overlay that pops into existence at last second over the thumbnail, basically hijacks your click and navigates you away from yt and to a support.google.com page that discusses paid product placement. No google thats not why I clicked on a video thumbnail.

By @felixfbecker - 3 months
I continue to be baffled how many websites rely on YouTube embeds to show videos, especially marketing/brand websites where you don't usually want other unrelated logos on the page. It's really not that hard to self-host and embed videos, there has been native support since HTML5.
By @p0w3n3d - 3 months
Lite YouTube Embed requires me to "start" the video twice - once click on embed, then another start on youtube loaded. However I'd like to see it fixed (if possible) because I find the lack of website load time optimization disturbing
By @gwern - 3 months
OP: the YT loading paradox you are thinking of is https://blog.chriszacharias.com/page-weight-matters
By @Aardwolf - 3 months
What I wonder is why some youtube embeds don't allow full screen (while being very small in the website so hard to watch), if you press full screen it then says something like "this website doesn't allow you to watch this video full screen". But on youtube this same video works fullscreen.

Are those websites themselves choosing that in a setting of their embed?

But why? It actually makes me navigate away from your website as I go to youtube instead to find the video there to view it properly there.

By @account42 - 3 months
> The Solution is to Replicate the Embed Experience Another Way.

No, the solution is to host your own videos and use a plain <video> element. Or it would be if Browser vendors would add native DASH/HLS-like adaptive streaming support to it, but even without that the user experience is much better with a <video>.

> Or link it from a CDN

Pure insanity.

By @ksec - 3 months
>TL;DR: YouTube Embeds are like 1.3MB in size with no shared resources between multiple embeds. Using a <lite-youtube> Web Component is more like 100k, does share resources, and sacrifices no functionality.

I would even go further and wonder why lite-youtube still requires 100K? And why they are not shared across different sites.

By @ZeljkoS - 3 months
Not mentioned in the article, one of the main reasons to ALWAYS do this is SEO. Regardless if users will play the video or not, web crawlers will not play the video and Google will penalize your SEO ranking if you use official Google's YouTube embed :D

We implemented our own thumbnail image trick on TestDome homepage a few years ago (https://www.testdome.com/). Thumbnail is from: https://img.youtube.com/vi/gPQQg4yZqt8/sddefault.jpg

By @system2 - 3 months
Why would you not use YoutubeLite for this? Just thumbnail loading then embed / play on click. You are reinventing the wheel for no reason.
By @demorilo - 3 months
What is the problem of sites hosting their own videos?
By @swyx - 3 months
i shared my solution here for anyone who wants an alternative to lite youtube embed (worked on it before knowing about paul's solution, has perhaps better tradeoffs with regards complete customizability)

https://swyxkit.netlify.app/faster-youtube-embeds

By @IshKebab - 3 months
IMO YouTube embeds are a bad experience anyway, especially on mobile. Just put a poster image with a link to the actual YouTube video.
By @occz - 3 months
To put this into context, the player element might load 1.3 mb but loading a video can easily be in the range of 50 mb.
By @pacifika - 3 months
A tiny script Click to toggle the display css attribute prevents it loading as the page loads
By @difosfor - 3 months
Shouldn't class lty-playbtn be lyt-playbtn?
By @didgetmaster - 3 months
Get the climate change activists involved. How many of these videos have to play before the wasted electricity is equivalent to a car running for a year?