OG Images in Gatsby
Gatsby renders every page to static HTML at build time, which is exactly what social crawlers want: the tags are in the HTML, with no JavaScript needed. Since gatsby@4.19.0, the built-in Gatsby Head API is the standard way to set them, replacing react-helmet for most sites.
This guide covers the Head API, the SEO component pattern from Gatsby's docs, making image URLs absolute with siteUrl, generating images yourself at build time, and the alternative: one Unfurlkit URL per page.
Sources checked (21 Oct 2026): Gatsby docs, "Gatsby Head API" reference (https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-head/) and "Adding an SEO Component" (https://www.gatsbyjs.com/docs/how-to/adding-common-features/adding-seo-component/).
Step 1: The Head API
Export a named Head function from a page, or from a template used with createPage, and return the tags:
Replace your-site.com with your own site.
// src/pages/about.jsx
export default function AboutPage() {
return <main>…</main>;
}
export const Head = () => (
<>
<title>About us</title>
<meta name="description" content="Who we are." />
<meta property="og:title" content="About us" />
<meta property="og:image" content="https://your-site.com/og/about.png" />
<meta name="twitter:card" content="summary_large_image" />
</>
);
Rules from Gatsby's reference that matter for OG tags:
Headonly works in pages and page templates, not in ordinary components. A shared SEO component is fine as long as a page'sHeadrenders it.- Valid tags are
link,meta,style,title,base,scriptandnoscript. - Deduplicate with
id. If two tags share anid, only the last one is rendered. That's how a page overrides a site-wide defaultog:image. - Head contents are cleared on unmount, so each page must define everything it needs.
Headreceiveslocation.pathname,params,data(from the page's GraphQL query) andpageContext.
Step 2: An SEO component with absolute URLs
Gatsby's SEO guide recommends putting site-wide values in siteMetadata, including siteUrl, the URL of your deployed site, "so that later metatags can point to absolute URLs." That matters for og:image: crawlers need an absolute URL.
Replace your-site.com with your own site.
// gatsby-config.js
module.exports = {
siteMetadata: {
title: 'My Blog',
description: 'Notes on building things.',
siteUrl: 'https://your-site.com',
image: '/og-default.png', // a file in /static
},
};
Following the docs' pattern, read it with a useSiteMetadata hook (a useStaticQuery on site.siteMetadata), then build an SEO component that takes optional props and falls back to the site values:
// src/components/seo.jsx
import * as React from 'react';
import { useSiteMetadata } from '../hooks/use-site-metadata';
export const SEO = ({ title, description, image, pathname, children }) => {
const site = useSiteMetadata();
const seo = {
title: title || site.title,
description: description || site.description,
image: image?.startsWith('http') ? image : `${site.siteUrl}${image || site.image}`,
url: `${site.siteUrl}${pathname || ''}`,
};
return (
<>
<title>{seo.title}</title>
<meta name="description" content={seo.description} />
<meta property="og:title" content={seo.title} />
<meta property="og:description" content={seo.description} />
<meta property="og:url" content={seo.url} />
<meta property="og:image" content={seo.image} id="og-image" />
<meta name="twitter:card" content="summary_large_image" />
{children}
</>
);
};
The docs recommend the prop || fallback pattern so no value ends up undefined. Use it on a blog post template:
// src/templates/blog-post.jsx
export const Head = ({ data, location }) => (
<SEO
title={data.markdownRemark.frontmatter.title}
description={data.markdownRemark.excerpt}
pathname={location.pathname}
/>
);
That gives every post correct tags but the same default image. Next, a unique image per post.
Step 3 (DIY): Generate images at build time
Gatsby has no built-in OG image generator. Your options are community plugins (check their maintenance status against your Gatsby version) or your own build step. A typical own-build approach:
- In
gatsby-node.js, when you callcreatePagefor each post, also render an image. A common stack is Satori (JSX to SVG) plus a PNG converter, or a headless browser screenshot. - Write the PNG into
public/og/<slug>.pngso it's deployed with the site. - Pass the image path to the template through
pageContext, and hand it toSEOasimage.
// gatsby-node.js (outline)
exports.createPages = async ({ graphql, actions }) => {
const { data } = await graphql(`{ allMarkdownRemark { nodes { id fields { slug } frontmatter { title } } } }`);
for (const post of data.allMarkdownRemark.nodes) {
const ogPath = `/og/${post.fields.slug}.png`;
await renderOgImage(post.frontmatter.title, `public${ogPath}`); // your renderer
actions.createPage({
path: post.fields.slug,
component: require.resolve('./src/templates/blog-post.jsx'),
context: { id: post.id, ogPath },
});
}
};
export const Head = ({ data, location, pageContext }) => (
<SEO title={data.markdownRemark.frontmatter.title} image={pageContext.ogPath} pathname={location.pathname} />
);
renderOgImage is yours to write. Make sure the public/og folder exists before writing.
The trade-offs
- Build time grows with every post. Rendering hundreds of images adds up on every full build unless you cache them between builds.
- Dependencies in CI. Satori needs fonts loaded explicitly (TTF/OTF/WOFF). PNG conversion or a headless browser adds native binaries.
- You maintain the template, the fonts and the layout code.
Step 3 (alternative): One URL with Unfurlkit
Instead of rendering, pass an Unfurlkit URL as image. The SEO component above already accepts absolute URLs:
// src/templates/blog-post.jsx
const ogUrl = (title) =>
'https://unfurlkit.com/v1/basic.png?' +
new URLSearchParams({ title: title || 'My Blog', theme: 'dark' }).toString();
export const Head = ({ data, location }) => (
<SEO
title={data.markdownRemark.frontmatter.title}
image={ogUrl(data.markdownRemark.frontmatter.title)}
pathname={location.pathname}
>
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
</SEO>
);
title is required and must be non-empty, so keep a fallback. The basic template also takes subtitle, theme (light or dark) and accent (6-digit hex, no #). See the basic template.
Plans. Every image URL needs your key. A Free key gets 10 renders in total (no monthly reset), with a small "unfurlkit" watermark, and needs no card. Pro is $9/mo or $90/yr, with 10,000 renders a month, no watermark, and custom fonts (Inter, Roboto, Playfair Display, JetBrains Mono) and logo. Pro URLs add your public key k and a signature s.
Signing in Gatsby. Head code also ships to the browser, so don't sign there. Sign in gatsby-node.js, which runs only at build time, and pass the finished URL in pageContext:
// gatsby-node.js (excerpt)
const { signedOgUrl } = require('./lib/unfurlkit'); // the Node snippet from our signing guide
actions.createPage({
path: post.fields.slug,
component: require.resolve('./src/templates/blog-post.jsx'),
context: {
id: post.id,
ogImage: signedOgUrl(
{ title: post.frontmatter.title || 'My Blog', theme: 'dark' },
process.env.UNFURLKIT_PUBLIC_KEY,
process.env.UNFURLKIT_SECRET_KEY,
),
},
});
Then use image={pageContext.ogImage} in Head. Don't give the secret a GATSBY_ prefix: Gatsby exposes those environment variables to browser code. See Signing Pro URLs.
A render is counted only when a unique image is generated and wasn't already cached. Shares and cache hits don't count. Over the limit, images keep rendering. On a signed Free key, images already made keep showing, and new ones get a plain version (your title on the basic card, with the small mark) that doesn't count. Upgrade to Pro for more renders. On Pro there's no hard stop and no overage charge, and we may contact you about your usage. Upgrade to Pro to remove watermarks from all your images. See pricing.
What changes:
- Builds stay fast. Nothing is rendered during
gatsby build. - No native dependencies in CI.
- Images follow titles. Change a title, rebuild, and the URL (and image) changes.
Which option should you use?
| Situation | Best fit |
|---|---|
| A small site, hand-made images | Files in static/ plus the SEO component |
| Custom-designed cards, and build time doesn't matter | Build-time rendering in gatsby-node.js |
| Many posts, fast builds, a clean standard card | Unfurlkit |
| You need right-to-left scripts today | Build-time rendering with suitable fonts (Unfurlkit doesn't support RTL yet) |
Debugging checklist
- Check the built HTML in
public/<path>/index.htmlforog:image. If it's missing, make sureHeadis exported from the page or template, not from a child component. - Duplicate
og:imagetags? Give the tag anidso a page's value replaces the default. - Relative image URL? Set
siteUrland prefix it. - Still using
react-helmet? Tags from both systems can conflict. Migrate toHead. - Unfurlkit 400? Missing or empty
title, or an invalid param. - Run the Free OG Image Checker to preview X, LinkedIn, Facebook, WhatsApp, Slack, Discord and iMessage.
See also OG image not showing.
FAQ
Which languages and scripts are supported? Right-to-left scripts such as Arabic and Hebrew aren't supported yet at launch.
Which Gatsby version do I need?
The Head API needs gatsby@4.19.0 or later. Editing <html> and <body> attributes from Head needs 5.5.0.
Can I keep react-helmet?
It still works, but Gatsby's docs say Head is easier to use, faster and smaller.
Where should the default image live?
In static/, referenced in siteMetadata.image and made absolute with siteUrl.
What size should the image be? 1200×630. See OG image size.
Summary
Use the Head API with an SEO component and an absolute siteUrl. For per-post images, render them in gatsby-node.js if you want full control, or pass an Unfurlkit URL and keep builds fast.
See Unfurlkit pricing · Check any page with the Free OG Image Checker.