HTML 3 min read

Embedding YouTube Videos and Google Maps Without Slowing Your Site

A single YouTube or Google Maps embed can add a large amount of JavaScript to your page. Learn how iframes work, how to lazy-load them and how to use lightweight facades for fast pages.

On this page

Key takeaways

  • A single YouTube embed can add 500 KB–1 MB+ of JavaScript before anyone presses play; a Google Maps embed is similar.
  • Use loading="lazy" on iframes and, better still, a lightweight facade that loads the real player on click.
  • Always give embeds a title and reserve their space to avoid layout shift.

Videos and maps make pages more useful — a product demo, a “how to reach our shop” map. But they’re loaded inside iframes that pull in the provider’s full application: scripts, fonts, tracking and images. On many sites I audit, embeds are the single biggest reason for poor mobile performance. Here’s how to keep them without the cost.

What an iframe is#

An <iframe> embeds another web page inside yours. The embedded page runs its own HTML, CSS and JavaScript, competing with your page for bandwidth and the main thread.

<iframe src="https://www.youtube-nocookie.com/embed/VIDEO_ID"
  title="How to set up Cloudflare for WordPress"
  width="560" height="315" loading="lazy"
  allow="accelerometer; autoplay; encrypted-media; picture-in-picture"
  allowfullscreen></iframe>

Level 1: native lazy loading#

Add loading="lazy" to any iframe below the fold. The browser waits until it’s near the viewport before loading it. WordPress adds this automatically to many embeds. It costs nothing and helps, but if the embed is in the first screen, it still loads immediately.

Level 2: facades (click to load)#

A facade shows a lightweight preview — the video thumbnail with a play button, or a static map image — and swaps in the real iframe only when the user clicks.

<button class="yt-facade" data-id="VIDEO_ID" aria-label="Play video: Cloudflare setup">
  <img src="https://i.ytimg.com/vi/VIDEO_ID/hqdefault.jpg" alt="" width="480" height="360" loading="lazy">
</button>
<script>
document.querySelectorAll('.yt-facade').forEach(function (b) {
  b.addEventListener('click', function () {
    var f = document.createElement('iframe');
    f.src = 'https://www.youtube-nocookie.com/embed/' + b.dataset.id + '?autoplay=1';
    f.title = b.getAttribute('aria-label');
    f.allow = 'autoplay; encrypted-media; picture-in-picture';
    f.allowFullscreen = true;
    f.width = 560; f.height = 315;
    b.replaceWith(f);
  });
});
</script>

Ready-made options include the lite-youtube web component and WordPress performance plugins with a “YouTube facade” or “lazy load videos” setting.

Need a hand with this?

Get a quick, honest review of your website’s SEO.

Chat on WhatsApp

Google Maps without the weight#

  • For a contact page, a static map image linking to Google Maps directions is often enough.
  • If you need the interactive map, lazy-load it and place it below the address and phone number.
  • Your address in text plus LocalBusiness schema matters more for local SEO than the embed itself.

Prevent layout shift#

.embed { aspect-ratio: 16 / 9; width: 100%; }
.embed iframe, .embed .yt-facade { width: 100%; height: 100%; border: 0; }

Reserving the box ensures nothing jumps when the video loads — good for CLS.

Accessibility and privacy#

  • Every iframe needs a descriptive title attribute.
  • Facade buttons need an accessible label (“Play video: …”).
  • Use youtube-nocookie.com to reduce tracking until the user plays.
  • Third-party embeds may set cookies; reflect them in your privacy policy and consent banner.

Video SEO tip#

If a video is the main content of a page, keep the facade’s thumbnail and add VideoObject structured data with a transcript or summary on the page so search engines still understand it. See video SEO and YouTube SEO.

Embeds FAQ#

Does lazy loading videos hurt SEO?#

No, as long as the page clearly contains the video (thumbnail, title, structured data). Google can still identify it.

Should I host videos myself instead of YouTube?#

Usually not. Self-hosting large videos costs bandwidth and needs adaptive streaming. YouTube with a facade, or a video platform like Bunny Stream or Vimeo, is more practical.

Why does PageSpeed Insights mention third-party code?#

Embeds count as third-party code. Facades remove that cost until a visitor chooses to play.

Found this useful? Share it

Join the discussion

Your email address will not be published. Required fields are marked *

Security code image. If you cannot read it, press refresh or contact me on WhatsApp.