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

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 }
    }
  }
`

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

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.

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.