How to Add OG Images to Gatsby — Complete Guide
Add dynamic Open Graph images to your Gatsby site. Covers the Gatsby Head API, gatsby-plugin-react-helmet, GraphQL data for dynamic OG images, and API-based generation.
Quick Answer
To add OG images in Gatsby, use the Gatsby Head API (Gatsby 4.19+) by exporting a Head function from your page component that returns og:image meta tags. For older versions, use gatsby-plugin-react-helmet. Point the og:image URL to an API like ogimg.xyz to auto-generate images from page data.
Gatsby Head API (Recommended)
Since Gatsby 4.19, the Gatsby Head API is the recommended way to manage document head tags. Export a Head function from any page component to add meta tags:
// src/pages/index.tsx
import * as React from "react";
export function Head() {
const title = "My Gatsby Site";
const description = "A blazing fast site built with Gatsby";
const ogImage = `https://ogimg.xyz/api/og?${new URLSearchParams({
title,
description,
template: "gradient",
}).toString()}`;
return (
<>
<title>{title}</title>
<meta name="description" content={description} />
<meta property="og:type" content="website" />
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:image" content={ogImage} />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta name="twitter:card" content="summary_large_image" />
</>
);
}
export default function IndexPage() {
return <main><h1>Welcome</h1></main>;
}
The Head API runs at build time, so the meta tags are baked into the static HTML — exactly what social media crawlers expect.
Dynamic OG Images with GraphQL Data
Gatsby's data layer uses GraphQL, which makes it easy to pull page data for dynamic OG images. Here's an example for MDX blog posts:
// src/pages/blog/{mdx.frontmatter__slug}.tsx
import * as React from "react";
import { graphql, PageProps } from "gatsby";
export function Head({ data }: PageProps<Queries.BlogPostQuery>) {
const post = data.mdx!;
const title = post.frontmatter!.title!;
const description = post.frontmatter!.description || "";
const ogImage = `https://ogimg.xyz/api/og?${new URLSearchParams({
title,
description,
template: "blog",
pattern: "dots",
}).toString()}`;
return (
<>
<title>{title}</title>
<meta name="description" content={description} />
<meta property="og:type" content="article" />
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:image" content={ogImage} />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta name="twitter:card" content="summary_large_image" />
</>
);
}
export const query = graphql`
query BlogPost($id: String!) {
mdx(id: { eq: $id }) {
frontmatter {
title
description
date
}
}
}
`;
export default function BlogPost({ data, children }: PageProps<Queries.BlogPostQuery>) {
return (
<article>
<h1>{data.mdx!.frontmatter!.title}</h1>
{children}
</article>
);
}
Every blog post now gets a unique OG image generated from its title and description. No manual image creation needed, and the images are generated on-demand — they don't increase your Gatsby build time.
Legacy Approach: gatsby-plugin-react-helmet
If you're on a Gatsby version older than 4.19 or prefer React Helmet, install the plugin:
npm install gatsby-plugin-react-helmet react-helmet
Add it to your gatsby-config.js:
// gatsby-config.js
module.exports = {
plugins: ["gatsby-plugin-react-helmet"],
};
Then use Helmet in your components:
import React from "react";
import { Helmet } from "react-helmet";
function SEO({ title, description }) {
const ogImage = `https://ogimg.xyz/api/og?${new URLSearchParams({
title,
description,
template: "gradient",
}).toString()}`;
return (
<Helmet>
<title>{title}</title>
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:image" content={ogImage} />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta name="twitter:card" content="summary_large_image" />
</Helmet>
);
}
Note: The Gatsby Head API is the officially recommended approach going forward. If you're starting a new Gatsby project, use the Head API instead of React Helmet.
Testing and Debugging OG Images in Gatsby
Gatsby generates static HTML at build time, so you can verify your OG tags before deploying:
- Build locally: Run
gatsby buildand check the generated HTML inpublic/. Open any HTML file and search forog:image. - Gatsby develop: During development, view source in your browser (Ctrl+U) and confirm the meta tags are present.
- Facebook Debugger: After deploying, paste your URL at
developers.facebook.com/tools/debugto see how Facebook renders your link preview. - Twitter Card Validator: Use
cards-dev.twitter.com/validatorto test Twitter card rendering.
Common issues to watch for:
- Missing tags in view source: Make sure you're using the Head API or React Helmet — not a
useEffectthat injects tags client-side. - Stale previews: Social platforms cache link previews aggressively. Use the Facebook Debugger's "Scrape Again" button to force a refresh.
- Relative URLs: The
og:imagevalue must be an absolute URL starting withhttps://.
Frequently Asked Questions
Does the Gatsby Head API work with Gatsby 5?
Yes. The Head API was introduced in Gatsby 4.19 and is fully supported in Gatsby 5. It's the recommended approach for managing head tags in all current Gatsby versions.
Do OG images slow down Gatsby builds?
No. When you use an API-based approach like ogimg.xyz, images are generated on-demand when social media crawlers request them. Nothing is generated at build time, so there is zero impact on your Gatsby build speed.
Can I use Gatsby Image for OG images?
No. OG images must be referenced by an absolute URL in a meta tag. Gatsby Image (gatsby-plugin-image) is for rendering optimized images in the page body, not for meta tags. Use an API URL or a static image URL for og:image.
Related Guides
Compare with Alternatives
See how OG Image Generator stacks up against other tools.
Start Generating OG Images
Free API for developers. 50 images/month, 10 templates, no credit card required.