Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better page load performance when loading multiple embedded Youtube videos?

I have a page which displays multiple (usually 10) embedded videos. The videos use the new IFRAME embed code of youtube and apparently for every IFRAME there is a separate request when loading the page. Is there a way to defer loading the videos after the rest of the page is loaded, so they don't slow down page loading that much?

like image 338
Tom Avatar asked Sep 29 '11 09:09

Tom


People also ask

Why do embedded YouTube videos slow down my website?

The embedded YouTube videos make your page heavy and slow down your page load times. The page load time is important factor for a web page because – Site Speed improves user experience. There are different surveys and case studies which show poor experience with slow sites.

How much data does a YouTube embedded video load?

A single YouTube embed can load up to 400KB of data before you even hit the play button. They also force the visitor’s browsers to make extra http requests. On a site with multiple videos embedded (like above), the extra weight can noticeably slow down load speeds (but even one embed makes a difference).

Does embedding videos on a web page affect page load time?

Here’s the short answer: It depends. The fact is that there are many ways to embed videos on a web page. The way you choose to go about doing it will have a significant impact on your web page load time. Here some examples of the different ways you can embed a video onto a web page today:

How to speed up YouTube video loading time?

Nevertheless, there are ways to speed up page loading time for embedded YouTube videos. Lazy loading is one of the most popular strategies in use today. It commands users’ web browsers to only load the part of the web page currently showing on the screen. Everything else is left to load later, when (or if) the user decides to scroll further.


2 Answers

Well, I wrote a javascript thingy (called "LYTE") that will create a "dummy player" (which looks & feels like a normal YouTube embed) for every div with a specific class ("lyte") and id with the YouTube-id. Only when clicking the "dummy" player, the actual YouTube iframe is loaded and it indeed has an important impact of the performance of pages that embed YouTube videos. You can see it in action on my blog.

LYTE is currently not really available standalone, only as part of WP-YouTube-Lyte, the WordPress plugin I wrote, but with minimal changes you should be able to extract all relevant code from the plugin.

You basically have to create something like this in your HTML;

<div class="lyte" id="7nuiOe8M6bw" style="width:640px;height:385px;">
<script type="text/javascript"><!-- 
var nT='newtube-';var bU='http://static.blog.futtta.be/wp-content/plugins/wp-youtube-lyte/lyte/';
var d=document;if(d.addEventListener){d.addEventListener('DOMContentLoaded', insert, false)}else{window.onload=insert}
function insert(){if(!d.getElementById('lytescr')){lytescr=d.createElement('script');lytescr.async=true;lytescr.id='lytescr';lytescr.src='http://static.blog.futtta.be/wp-content/plugins/wp-youtube-lyte/lyte/lyte-min.js';h=d.getElementsByTagName('script')[0];h.parentNode.insertBefore(lytescr, h)}}; 
 --></script></div>

This block will load lyte-min.js, which will fill the div with all graphical elements of the dummy player (image, play button, control bar, title) and will add an eventlistener to the div that will trigger the replacement of the div with the real embedded player when clicked.

You can find the plugin here and look at the code here (wp-youtube-lyte.php creates the div, lyte/lyte.js is the actual javascript ... thingy).

like image 145
futtta Avatar answered Oct 11 '22 04:10

futtta


Check out this solution: Load the YouTube Video Player On-Demand

Basically, it's like replacing your YouTube video iframe player codes with jQuery when clicking some thumbnails.

like image 32
David Avatar answered Oct 11 '22 04:10

David