🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Performance••By Komal Nakrani

Lazy-Load Iframes and Embeds in Astro

Lazy-load iframes in Astro: native loading=lazy for maps and forms, a click-to-load facade for YouTube, reserved space to stop layout shift, and astro-embed.

#Astro#Performance#Lazy Loading#Iframes

To lazy-load an iframe in Astro, add loading="lazy" with a width and height (or a CSS aspect-ratio), and the browser waits until the iframe is near the viewport before loading it. For heavy embeds such as YouTube players, go one step further with a facade: render a thumbnail and a play button as plain HTML, and only insert the real iframe when the visitor clicks.

Key Takeaways

  • loading="lazy" is native on iframes in all current browsers; no script needed.
  • Never lazy-load an iframe that is visible on first load, since that delays it.
  • Reserve space with aspect-ratio so the embed does not shift the layout.
  • A click-to-load facade saves the most for video players, which load large scripts even before playback.

Native lazy loading

---
// src/components/MapEmbed.astro
interface Props {
  src: string;
  title: string;
}
const { src, title } = Astro.props;
---
<iframe
  src={src}
  title={title}
  loading="lazy"
  width="600"
  height="450"
  style="border:0; width:100%; height:auto; aspect-ratio: 4 / 3;"
  referrerpolicy="no-referrer-when-downgrade"
></iframe>

This suits maps, booking widgets and forms lower on the page. The title attribute is required for accessibility; screen readers announce it. Width, height and aspect-ratio keep space reserved, which protects your Cumulative Layout Shift score. The layout shift guide covers other causes.

A click-to-load YouTube facade

A YouTube iframe loads player scripts as soon as it loads, even if nobody presses play. A facade shows the video thumbnail instead and swaps in the iframe on click:

---
// src/components/YouTube.astro
interface Props {
  id: string;
  title: string;
}
const { id, title } = Astro.props;
---
<div class="yt" data-id={id} data-title={title}>
  <img
    src={`https://i.ytimg.com/vi/${id}/hqdefault.jpg`}
    alt=""
    loading="lazy"
    width="480"
    height="360"
  />
  <button type="button" aria-label={`Play video: ${title}`}>Play</button>
</div>

<script>
  document.querySelectorAll<HTMLElement>('.yt').forEach((el) => {
    el.querySelector('button')?.addEventListener('click', () => {
      const iframe = document.createElement('iframe');
      iframe.src = `https://www.youtube-nocookie.com/embed/${el.dataset.id}?autoplay=1`;
      iframe.title = el.dataset.title ?? 'YouTube video';
      iframe.allow = 'accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture';
      iframe.allowFullscreen = true;
      el.replaceChildren(iframe);
    }, { once: true });
  });
</script>

<style>
  .yt { position: relative; aspect-ratio: 16 / 9; background: #000; }
  .yt img, .yt :global(iframe) { width: 100%; height: 100%; object-fit: cover; border: 0; }
  .yt button { position: absolute; inset: 0; margin: auto; width: 4rem; height: 4rem; }
</style>

The youtube-nocookie.com domain avoids setting YouTube cookies until playback, which can simplify your cookie consent setup. The :global(iframe) selector is needed because the iframe is created by script, so it does not carry the component’s scoping attribute.

If you would rather not maintain this yourself, the community astro-embed package provides YouTube and Vimeo components that use the same facade idea, plus components for posts from X, Bluesky and Mastodon.

Lazy-load any embed script with an island

Some embeds are a script, not an iframe. Wrap them in a framework component and hydrate it with client:visible, so the code only loads when the embed scrolls into view:

---
// src/pages/contact.astro
import BookingWidget from '../components/BookingWidget.jsx';
---
<BookingWidget client:visible />

The lazy-load islands tip explains client:visible options such as rootMargin.

When not to lazy-load

An iframe at the top of the page, such as a hero video, should load normally. Lazy loading only delays it, and a large embed above the fold may become your Largest Contentful Paint element. Use a poster image for the hero and load the player on click instead. Our theme catalog themes use static images for heroes for the same reason. For more on embeds in components, see the pages docs.

FAQ

Does loading=“lazy” work on iframes?

Yes. All current major browsers support loading="lazy" on <iframe> elements. Browsers decide the exact distance from the viewport at which loading starts.

Will lazy-loaded iframes hurt SEO?

No. Search engines do not need your iframes to understand the page, and Google renders lazy-loaded content that uses native lazy loading. Put important text in the page itself, not inside an embed.

How do I stop an embed from shifting the layout?

Give the iframe width and height attributes or a CSS aspect-ratio, so the browser reserves the space before the content loads.

Is a YouTube facade worth it?

On pages where most visitors do not play the video, yes. The facade loads one thumbnail image instead of the player’s scripts and frames, which makes the page lighter until someone presses play.