Updated 2026-11-24
Gatsby SEO: the Head API, canonical URLs, JSON-LD and the sitemap
We checked 1,208 pages on 102 Gatsby sites. 63% had no JSON-LD, 42% no canonical link, 33% no og:image, 17% no lang on <html>, 14% no meta description and 8% no <title> at all. Much of that comes from one rule: Gatsby writes head tags only for page components that export Head, and a layout can't do it for them. This guide sets up one Seo component that every page and template uses, then adds JSON-LD, the sitemap and a check of the build.
Tested with a full gatsby build on Gatsby 5.16.1, React 18, gatsby-transformer-remark and gatsby-plugin-sitemap: the snippets below produce the tags, JSON-LD and sitemap files described here.
1. Why pages end up with no title
Headonly works in pages and in templates passed tocreatePage, not in components. A<title>inside your layout's JSX doesn't reach<head>: it is rendered into<body>with the page. Every file insrc/pages/and every template needs its ownexport const Head.- The pages people forget are the generated ones: tag and category listings, paginated blog indexes and the 404 page.
- react-helmet (
gatsby-plugin-react-helmet) still works, but the Head API replaces it and needs no plugin. Don't use both on one page, or you get two sets of tags.
2. One Seo component for every page
// gatsby-config.js
module.exports = {
siteMetadata: {
title: "Example Docs",
siteUrl: "https://docs.example.com", // no trailing slash
image: "/og-default.png", // in static/, about 1200×630
},
plugins: ["gatsby-plugin-sitemap"],
}
// src/components/seo.jsx
import * as React from "react"
import { useStaticQuery, graphql } from "gatsby"
export function Seo({ title, description, pathname, image, children }) {
const { site } = useStaticQuery(graphql`
query { site { siteMetadata { title siteUrl image } } }
`)
const m = site.siteMetadata
const url = m.siteUrl + (pathname || "/")
const img = m.siteUrl + (image || m.image)
const full = title ? `${title} | ${m.title}` : m.title
return (
<>
<html lang="en" />
<title>{full}</title>
{description && <meta name="description" content={description} />}
<link rel="canonical" href={url} />
<meta property="og:title" content={title || m.title} />
{description && <meta property="og:description" content={description} />}
<meta property="og:url" content={url} />
<meta property="og:image" content={img} />
<meta property="og:type" content="website" />
<meta name="twitter:card" content="summary_large_image" />
{children}
</>
)
}
In a template, pass the page's own text and the location prop that Gatsby gives Head:
// src/templates/doc.jsx
import { graphql } from "gatsby"
import { Seo } from "../components/seo"
export const Head = ({ data, location }) => (
<Seo
title={data.markdownRemark.frontmatter.title}
description={data.markdownRemark.frontmatter.description || data.markdownRemark.excerpt}
pathname={location.pathname}
/>
)
export const query = graphql`
query ($id: String!) {
markdownRemark(id: { eq: $id }) {
excerpt(pruneLength: 155)
frontmatter { title description }
}
}
`
- Don't fall back to one site-wide description. The official blog starter (gatsby-starter-blog) does, and then hundreds of pages share it. A page's
excerptis a better fallback; no tag beats a duplicate. Duplicate titles and descriptions on a docs site explains why. <html lang="en" />insideHeadsets the attribute on the real<html>(Gatsby 5.5 and later). Gatsby's defaulthtml.jssets nolang. On older versions, usesetHtmlAttributesinonRenderBodyingatsby-ssr.js.- Canonical and trailing slashes: Gatsby 5's
trailingSlashoption defaults to"always", solocation.pathnameis/guide/install/and the canonical matches the URL Gatsby serves. KeepsiteUrlwithout a trailing slash, or you get//guide. og:imagemust be absolute, which is why the component prefixessiteUrl. If a card still shows no picture, see og:image not showing.
3. JSON-LD that survives any title
Pass it as a child of Seo:
export const Head = ({ data, location }) => {
const p = data.markdownRemark
const ld = {
"@context": "https://schema.org",
"@type": "TechArticle",
headline: p.frontmatter.title,
description: p.frontmatter.description || p.excerpt,
url: "https://docs.example.com" + location.pathname,
}
return (
<Seo title={p.frontmatter.title} description={p.frontmatter.description || p.excerpt} pathname={location.pathname}>
<script type="application/ld+json">
{JSON.stringify(ld).replace(/</g, "\\u003c")}
</script>
</Seo>
)
}
Why the .replace: Gatsby renders Head to a string, parses it, and writes each script's decoded text back into the page as raw HTML. Quotes and & come through fine, but a < comes through too. A title like Fixing </script> errors ends the script tag early: the JSON-LD breaks and the rest of the title shows up as page text. With < written as <, the JSON stays the same for parsers and the tag can't close early. Use WebSite on the home page and BreadcrumbList on deep pages the same way; JSON-LD for docs and blogs covers which types fit.
4. Sitemap and robots.txt
- gatsby-plugin-sitemap writes
/sitemap-index.xmlplussitemap-0.xml, not/sitemap.xml. Submit the index in Search Console, and inrobots.txtpoint to that file, not the usual name. - It reads
siteMetadata.siteUrland only runs ingatsby build, not ingatsby develop. - Every page is listed except
/404and Gatsby's internal pages. If you add<meta name="robots" content="noindex" />to a page'sHead, also list its path in the plugin'sexcludesoption (minimatch globs, e.g."/drafts/**"). - robots.txt: put it in
static/, which is copied to the site root:User-agent: * Allow: / Sitemap: https://docs.example.com/sitemap-index.xml
5. Check the build
gatsby build
grep -rL --include=index.html '<title' public
grep -rL --include=index.html 'name="description"' public
grep -rL --include=index.html 'rel="canonical"' public
grep -rhoE --include=index.html 'name="description" content="[^"]*"' public | sort | uniq -c | sort -rn | head
The first three list pages with no title, description or canonical; public/404/index.html showing up is fine. The last one shows descriptions shared by several pages. To see a deployed page the way Google and link previewers do, run it through the free SEO checker, or up to 50 pages from your sitemap through the site checker.
Automating it with Headlark
Writing a description for each of hundreds of generated pages is the slow part, and leaving them empty is what we found on most Gatsby sites. Headlark's build step runs after gatsby build on the HTML in public/ and writes the missing tags from each page's own content. Add it once with npm i -D headlark, then:
gatsby build && npx headlark public --site-url https://docs.example.com
Use the same address as siteUrl. Set HEADLARK_KEY in the build environment.
- Each page gets a title, a description, Open Graph and X tags written from its own content, and alt text for images that have none.
- Your canonical, robots tags,
og:imageand JSON-LD types are kept. Headlark doesn't create images. - Gatsby re-applies the tags from your
Headexport when a page loads in a browser. So set the tags you want to control by hand inHead, and Headlark fills in the pages whereHeadwrites nothing.
Try it
Try it free for 14 days on one site and up to 500 pages, no card needed. Add your site, copy its secret key, and your next build ships with SEO.