A better YouTube video embed

A better YouTube video embed

Showing YouTube thumbnail first and upon clickinig it we will load the iframe saving a lot of resources on page load.

svelte logo

Want a Svelte site built? Hire Me.

Whenever you embed a youTube video on a site, the page load takes a significant amount of time and makes a lot of requests. I wanted to avoid that. I found that the video iframe can be loaded on demand.


Youtube iframe loads a lot of things which takes time and resources. What if we can circumbent that.

Not all videos you embed on a website are watched by your users

Even after the embed, users usually see a thumbnail with play button which we can provide manually.

YouTube Thumbnail URL

Getting youtube thumbnail is easy if you have the video ID. This can also be done through YouTube API but it is an overkill for what we want to achieve here.


Refer this answer

I just add a play button over this image and on click I will replace the image with iframe.


  let play = false;

{#if play}
  <iframe src="https://www.youtube.com/embed/q2Y3f0lHnMs"></iframe>
  <img src="https://img.youtube.com/vi/q2Y3f0lHnMs/maxresdefault.jpg" />

The image loads faster on subsequent page load because it is cached by the browser!


Find related articles

Let's discuss on Twitter