Get started with 33% off your first certification using code: 33OFFNEW

How to embed YouTube videos that pass Core Web Vitals

2 min read
Published on 24th January 2024

Embedding YouTube videos on websites is commonplace. However, this practice often comes at a cost to website performance, particularly impacting Core Web Vitals. Core Web Vitals are a set of metrics by Google that measure the health and user experience of a website. One way to embed YouTube videos without sacrificing these vital metrics is by using the lite-youtube-embed package. Let’s explore how.

Understanding the Challenge

Embedding videos directly from YouTube can significantly impact your website's loading time, as it loads multiple scripts and resources, even if the user doesn’t play the video. This can negatively affect your site's Core Web Vitals scores, particularly Largest Contentful Paint (LCP) and First Input Delay (FID).

Enter YouTubeLite

YouTubeLite is a lightweight solution for embedding YouTube videos. It defers loading the heavy YouTube player until the user actually clicks on the video. Initially, it loads a static image in place of the video, which is substantially smaller in size.

Key Benefits of YouTubeLite

  1. Improved LCP Score: By loading a static image instead of the full video player, the LCP score improves as the largest content becomes lighter and loads faster.
  2. Reduced Page Weight: The initial load of the page is lighter, enhancing the overall page speed.
  3. Enhanced User Experience: Users enjoy a faster site with on-demand video loading, leading to a better overall experience.

How to Use YouTubeLite

  1. Install the Package: First, install YouTubeLite. If you are using npm, you can install it via your terminal:
npm i lite-youtube-embed
  1. Embed the Video: Once installed, you can embed a YouTube video by simply adding an element with a class of youtubelite and a data-youtube-id attribute.
<lite-youtube videoid="VIDEO_ID"></lite-youtube>

Replace VIDEO_ID with the ID of the YouTube video you wish to embed.

  1. Initialize YouTubeLite: In your JavaScript file, initialize YouTubeLite:
<link rel="stylesheet" href="node_modules/lite-youtube-embed/src/lite-yt-embed.css" />

<script src="node_modules/lite-youtube-embed/src/lite-yt-embed.js"></script>

You can also do this via your bundler of choice, such as Webpack or Vite.

Customization and Optimization Tips

  • Lazy Loading: Consider implementing lazy loading for off-screen videos to further improve loading times.
  • Custom Thumbnails: Use custom thumbnails that are optimized for size and quality to maintain a visually appealing design.
  • Placeholder Styling: Style the video placeholder to match your website’s design and aesthetics.

Embedding YouTube videos using the YouTubeLite package is a smart way to maintain high Core Web Vitals scores while still offering rich video content. This method significantly reduces the impact on page load times, providing a user-friendly experience without compromising on the functionality. By adopting YouTubeLite, you ensure that your website remains fast, engaging, and SEO-friendly.