To add Open Graph tags in Astro, put <meta property="og:..."> tags in a head component that every layout uses, and build og:image and og:url as absolute URLs with new URL(path, Astro.site). Set site in astro.config.mjs first, because social platforms ignore relative image paths. With og:title, og:description, og:image, og:url and og:type in place, links to your pages show a title, summary and image in Slack, LinkedIn, Facebook, iMessage and most chat apps.
Key Takeaways
- Open Graph needs absolute URLs; set
siteand build them fromAstro.site.- Use a 1200 x 630 px image under a few hundred kB, in JPG or PNG.
- Add
og:typeofarticleplusarticle:published_timeon blog posts.- Platforms cache previews; re-scrape with their debug tools after changes.
The tags in a head component
---
// src/components/OpenGraph.astro
interface Props {
title: string;
description: string;
image?: string;
type?: 'website' | 'article';
publishedTime?: Date;
}
const {
title,
description,
image = '/og/default.jpg',
type = 'website',
publishedTime,
} = Astro.props;
const url = new URL(Astro.url.pathname, Astro.site);
const imageUrl = new URL(image, Astro.site);
---
<meta property="og:type" content={type} />
<meta property="og:site_name" content="Example Studio" />
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:url" content={url} />
<meta property="og:image" content={imageUrl} />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta property="og:image:alt" content={title} />
<meta property="og:locale" content="en_US" />
{type === 'article' && publishedTime && (
<meta property="article:published_time" content={publishedTime.toISOString()} />
)}
Render it inside <head> in your base layout and pass the page’s values down. Astro converts the URL objects to strings in the attribute. If Astro.site is undefined because site is not set, new URL() throws during the build, which is a useful reminder:
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
site: 'https://www.example.com',
});
The config docs show where this lives in a theme project.
Choosing the image
| Property | Recommendation |
|---|---|
| Size | 1200 x 630 px (1.91:1) |
| Format | JPG or PNG; some platforms ignore WebP and SVG |
| File size | Keep it small; several platforms skip very large images |
| Text | Large, few words, away from the edges |
Put a default image in public/og/ for pages without their own. For blog posts, a per-post image works better; our OG image generation guide builds one for every post at build time with Satori.
If you import the image from src/assets/ instead, use getImage() to get an optimized URL, then pass optimized.src through new URL(). Set format: 'jpg' so the output is a format every platform reads.
Blog posts as articles
On posts, set type="article" and pass the publish date. Platforms such as LinkedIn and Facebook use the type to label the preview. You can also add article:modified_time, article:author and article:tag tags. Keep og:title as the plain post title, without your site name suffix; the platform shows og:site_name separately.
Test the previews
Build and deploy, since debuggers need a public URL. Then check each platform:
- Facebook: the Sharing Debugger shows the tags it found and lets you scrape again.
- LinkedIn: the Post Inspector refreshes LinkedIn’s cache.
- Slack and chat apps: paste the link in a private channel; most cache for a while, so add a query string such as
?v=2to force a fresh fetch while testing.
Locally, run npm run build and search dist/ for og:image to confirm every page has an absolute URL. X uses its own twitter: tags with Open Graph as a fallback; see the Twitter cards tip. For the full set of head tags, our meta tags component guide puts Open Graph, canonical and robots in one component. If you start from a theme in our catalog, look for the component that renders <head> in the base layout; that is the one file to edit.
FAQ
Does Astro add Open Graph tags automatically?
No. Astro renders what you put in <head>. Add the tags in a shared head component so every page gets them.
Why is my og:image not showing?
Usually the URL is relative, the image is too large, or the format is not supported. Use an absolute HTTPS URL to a 1200 x 630 JPG or PNG, then re-scrape the page in the platform’s debugger.
What is the difference between Open Graph and Twitter cards?
Open Graph is used by Facebook, LinkedIn, Slack and most other platforms. X reads twitter: tags first and falls back to Open Graph for title, description and image, so you can keep the twitter: tags minimal.
Do Open Graph tags help SEO?
Not directly as a ranking factor. They make shared links more attractive, which can bring more clicks and links over time.