🔥 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)
Content & MDX••By Komal Nakrani

Astro YouTube Embed: The Lightweight Way in MDX

Add a lightweight Astro YouTube embed to Markdown and MDX with astro-embed: one import, a thumbnail facade, privacy-friendly playback and no player until click.

#Astro#YouTube Embed#MDX#Performance

The quickest lightweight YouTube embed in Astro is the YouTube component from @astro-community/astro-embed-youtube. Import it in an MDX file, write <YouTube id="VIDEO_ID" />, and the page shows a thumbnail with a play button; the real YouTube player only loads after a click, using the privacy-enhanced youtube-nocookie.com domain.

Key Takeaways

  • A standard YouTube iframe loads the full player on page load, even if nobody presses play.
  • @astro-community/astro-embed-youtube renders a facade based on lite-youtube-embed.
  • It accepts a video ID or a full YouTube URL.
  • Pass it through components so authors don’t repeat the import in every post.

Install and use it in MDX

npm install @astro-community/astro-embed-youtube
---
title: "Deploying to Cloudflare Pages"
---
import { YouTube } from '@astro-community/astro-embed-youtube';

Here's the whole process in four minutes:

<YouTube id="https://youtu.be/dQw4w9WgXcQ" />

The component outputs a <lite-youtube> element with the video’s thumbnail as background and a small script that swaps in the iframe on click. Before the click, the page loads one image from i.ytimg.com instead of the player’s scripts, styles and tracking requests.

It also works in .astro files with the same import. Check the project’s README for the full prop list; the most useful are poster for a custom thumbnail and params for player parameters such as a start time (params="start=30").

Make it available in every post

Repeating the import in every MDX file gets tedious. Pass the component when you render the entry instead:

---
// src/pages/blog/[id].astro (excerpt)
import { render } from 'astro:content';
import { YouTube } from '@astro-community/astro-embed-youtube';

const { post } = Astro.props;
const { Content } = await render(post);
---
<Content components={{ YouTube }} />

Now any MDX post can use <YouTube id="..." /> without an import line. This works for any component you want authors to reach, such as callouts or figures.

Plain Markdown files

.md files can’t render components. Your options:

  1. Convert the post to .mdx (rename it and make sure @astrojs/mdx is installed).
  2. Paste an iframe as raw HTML with loading="lazy". It’s heavier, but it works anywhere.
  3. Use a frontmatter field such as video: dQw4w9WgXcQ and render the component from the layout.
<iframe
  src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ"
  title="Deploying to Cloudflare Pages"
  loading="lazy"
  width="560"
  height="315"
  allow="accelerometer; encrypted-media; gyroscope; picture-in-picture; fullscreen"
></iframe>

Option 3 is tidy for posts built around one video, since the layout controls placement and you can add VideoObject structured data in the same place.

When to build your own

The package is the fastest route. Write your own facade if you need markup that works with JavaScript disabled (a real link to the video), custom styling of the play button, or a self-hosted poster optimized with <Image />. Our full YouTube embeds guide builds that component step by step and covers thumbnail sizes, Content Security Policy entries and structured data.

Whichever route you take, reserve the space with a 16:9 aspect-ratio so the embed doesn’t shift the layout as it loads; the layout shift guide explains why that matters for CLS. If you’re picking a theme for a video-heavy blog, the Blog & Editorial category themes render MDX through <Content />, so the components approach above drops straight in.

FAQ

How do I embed a YouTube video in Astro MDX?

Install @astro-community/astro-embed-youtube, import { YouTube } at the top of the MDX file, and add <YouTube id="VIDEO_ID" /> where the video should go. The id can also be a full YouTube URL.

Does astro-embed YouTube use youtube-nocookie?

Yes. It’s built on the lite-youtube-embed approach, which loads the player from youtube-nocookie.com only after the visitor clicks, so no YouTube cookies or player scripts load before then.

Can I use the YouTube component in a .md file?

No. Plain Markdown can’t import components. Rename the file to .mdx, embed a raw <iframe> with loading="lazy", or render the component from your layout using a frontmatter field.