How can I speed up page load time with YouTube embeds?

Hi, I have a YouTube embeded video on some of my product pages and it increased my page load time by 4-7 seconds. I know that I can add the media through Shopify instead, but I do not want to go that route and I read that it doesn’t help much.

So, does anyone know how to have my YouTube embeds load after everything else? Or at least speed up the page load time while keeping the video?

Couple things you can do:

A) Lazy load youtube videos, you do it the same way you lazy load images (reference: https://speedboostr.com/shopify-lazy-loading). This will help.

B) The best way, but a little more effort is to create a static image with a play button and load that, then set a JavaScript listener for click on the image and when that gets clicked, replace the image with the embedded video. I don’t have a tutorial for this one but just send that explanation to your developer, they should be able to figure it out. If you get stuck we have a service called Deferred Video Loading to solve that: https://speedboostr.com/services.

If editing yourself, remember to duplicate your live theme first and work off the new backup theme while you’re editing, so any changes don’t affect the live site + you can measure the before / after to verify you’ve done it correctly and gained speed.

1 Like

Unfortunately, I already tried adding the code through this tutorial (https://www.annacantrell.com/how-to/how-to-defer-loading-iframe-content/) to defer the

I found a solution/work-around to lazy load the embeds on most popular browsers (Google Chrome, Firefox and soon to be Safari) without needed to add any JavaScript.

Just add loading=“lazy” after <iframe and it works without any other coding needed.

Example:

This also works for images by putting loading=“lazy” after <img.

I found an even better solution than just lazy loading. It is called the Facade Method and it SIGNIFICANTLY improves page load time, while still allowing for the YouTube embed.

Here’s how to do it: https://community.shopify.com/c/Technical-Q-A/How-To-Implement-the-Facade-Method-for-YouTube-Embedded-Videos/m-p/1289873

Any idea why doing this would cause the video not to work anymore?

It depends what you mean by “not work anymore”.

I would include a url and a description of exactly what isn’t working. Someone should be able to investigate and see why.

I’d guess that your javascript is broken and not loading the video container correctly when the user scrolls.