To embed a tweet in Astro without slowing the page, use the Tweet component from @astro-community/astro-embed-twitter. It fetches the post during the build and renders it as a static <blockquote> with text, author and link, so visitors never download X’s widgets.js. Bluesky posts work the same way with @astro-community/astro-embed-bluesky.
Key Takeaways
- X’s official embed script loads extra JavaScript, iframes and tracking on every page view.
astro-embedrenders posts to HTML at build time and ships no script.- Posts are fetched once per build, so a deleted post stays on your page until the next build.
- For any oEmbed provider, a 20-line component can fetch the embed HTML at build time.
Embed an X post
npm install @astro-community/astro-embed-twitter
---
title: "What people said about the launch"
---
import { Tweet } from '@astro-community/astro-embed-twitter';
<Tweet id="https://x.com/USERNAME/status/POST_ID" />
Pass the full post URL as id (a twitter.com URL works too). The component calls X’s public oEmbed endpoint during the build and outputs a blockquote with the post text, author, date and a link back. Style it with your own CSS; the package also ships optional default styles described in its README.
Because it’s fetched at build time, the build needs network access, and an outage on X’s side can fail the fetch. Pin important quotes as plain text with a link if your build must be reproducible offline.
Embed a Bluesky post
npm install @astro-community/astro-embed-bluesky
import { BlueskyPost } from '@astro-community/astro-embed-bluesky';
<BlueskyPost id="https://bsky.app/profile/HANDLE/post/POST_ID" />
It also renders static HTML at build time, including images attached to the post. Check the package README for the current props.
Build your own oEmbed component
Many platforms publish an oEmbed endpoint that returns ready-made embed HTML. You can call it at build time from an Astro component:
---
// src/components/OEmbed.astro
interface Props {
url: string;
endpoint: string;
}
const { url, endpoint } = Astro.props;
const api = new URL(endpoint);
api.searchParams.set('url', url);
api.searchParams.set('omit_script', '1');
let html = '';
try {
const res = await fetch(api);
if (res.ok) html = (await res.json()).html ?? '';
} catch {
// fall through to the plain link
}
---
{html ? <div class="oembed" set:html={html} /> : <p><a href={url}>View the post</a></p>}
<OEmbed
url="https://x.com/USERNAME/status/POST_ID"
endpoint="https://publish.twitter.com/oembed"
/>
omit_script=1 tells X not to include the widgets.js tag, so you get only the blockquote. Other providers ignore that parameter; for Mastodon, the endpoint is https://<instance>/api/oembed, which returns an iframe. Only use set:html with providers you trust, since it inserts their HTML as-is. The fallback link means a failed fetch never breaks the build. Our fragment and set:html tip explains when raw HTML is safe.
Make components available in every post
Rather than importing in every MDX file, pass them when rendering:
---
// src/pages/blog/[id].astro (excerpt)
import { render } from 'astro:content';
import { Tweet } from '@astro-community/astro-embed-twitter';
import { BlueskyPost } from '@astro-community/astro-embed-bluesky';
const { post } = Astro.props;
const { Content } = await render(post);
---
<Content components={{ Tweet, BlueskyPost }} />
Static embed or official widget?
| Static embed (build time) | Official widget script | |
|---|---|---|
| JavaScript shipped | None | Platform’s script plus iframes |
| Likes and replies counts | Not shown or frozen at build | Live |
| Deleted posts | Stay until next build | Disappear |
| Tracking and cookies | None from the platform | Platform-dependent |
| Layout shift | None | Common while the widget loads |
Static embeds suit articles that quote posts as sources. If live counts matter, load the official widget only on that page and reserve space for it. If a CSP blocks the widget, see our Content Security Policy guide. For video, the lightweight YouTube embed tip uses the same astro-embed project. If you’re starting a new blog or magazine site, add these components in the post template once.
FAQ
How do I embed a tweet in Astro?
Install @astro-community/astro-embed-twitter, import { Tweet }, and add <Tweet id="POST_URL" /> in an MDX or .astro file. It renders the post as static HTML at build time without X’s embed script.
Why use a static tweet embed instead of the X embed code?
The official embed loads widgets.js and an iframe per post, which adds JavaScript, tracking and layout shift. A static embed is plain HTML that loads instantly, at the cost of live like and reply counts.
What happens if the embedded post is deleted?
A static embed keeps showing the content captured at the last build. Rebuild the site to refresh it, or remove the embed. With the official widget, a deleted post disappears immediately.