Free auditStart free
GuideGatsby · Framework guide

Gatsby SEO

Use Gatsby's Head API for page metadata, verify generated HTML, and keep sitemap URLs aligned with published routes.

3 min
read
5
sources
Oct 24, 2026
updated
Scaplet Editorial Team
author
In one line

Use Gatsby's built-in Head export on pages and createPage templates to render page-specific title, description, canonical, and social metadata into generated HTML. Support for Head was added in gatsby@4.19.0; older projects need a compatible alternative. Set siteUrl, add gatsby-plugin-sitemap, then build and inspect both the generated HTML and the sitemap.

30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode
01Checklist

Render metadata with Gatsby Head

Gatsby's Head export adds page-specific tags to the generated HTML. Gatsby's docs say support was added in gatsby@4.19.0. The Head function receives location.pathname, params (for matchPath pages), data from the page's GraphQL query, and pageContext, which makes it useful for CMS-backed templates created with createPage. Valid tags inside it are link, meta, style, title, base, script and noscript.
First2
Export Head from the page or its createPage template. Gatsby's docs say you can only define it inside a page (templates for createPage included), not in a component.
Use page query data or pageContext for route-specific metadata. Add fallbacks for missing optional fields without repeating one generic title across every page.
Next1
Give tags an id when you need only one of them rendered: in a list of items with the same id, the last item wins and is used in the HTML.
Then1
For Gatsby versions older than 4.19.0, upgrade or use a metadata method that version supports. Editing <html> and <body> attributes from Head needs gatsby@5.5.0, and reading React Context from wrapRootElement needs gatsby@5.6.0.
A page-level Head export
export function Head({ data, location }) {
  const page = data?.contentfulPage
  const title = page?.seoTitle || page?.title || 'Example site'
  const description = page?.seoDescription || page?.summary
  const canonical = `https://www.example.com${location.pathname}`

  return (
    <>
      <title>{title}</title>
      {description && <meta name="description" content={description} />}
      <link rel="canonical" href={canonical} />
    </>
  )
}
02How it works

Generate a sitemap from the production route set

  1. 1

    Set the production site URL

    Define siteUrl in siteMetadata using the canonical HTTPS origin. Gatsby's guide says it must be defined and not left empty; the plugin README says it is needed unless you set the resolveSiteUrl option.

  2. 2

    Add the official Gatsby sitemap plugin

    Install gatsby-plugin-sitemap and include it in the plugins array in gatsby-config.js or gatsby-config.ts.

  3. 3

    Run a production build

    Run gatsby build && gatsby serve; the plugin only generates output in production mode. By default it writes sitemap-index.xml at the site root, plus a new sitemap-X.xml for every 45000 URLs (entryLimit). Point Search Console at https://www.example.com/sitemap-index.xml.

  4. 4

    Compare sitemap URLs with deployed pages

    Check representative static and CMS-generated routes. Remove drafts, previews, duplicates, and routes that redirect or return errors.

gatsby-config.js
module.exports = {
  siteMetadata: {
    siteUrl: 'https://www.example.com',
  },
  plugins: ['gatsby-plugin-sitemap'],
}

If your pages come from a CMS, confirm that the Gatsby build created them before expecting the sitemap plugin to list them. By default the sitemap includes all of the site's pages except those you exclude via excludes; /dev-404-page, /404 and /offline-plugin-app-shell-fallback are always excluded. The default output gives every URL the same changefreq and priority, which Google ignores. The README recommends a custom serialize that supplies an accurate lastmod.

03Validate the page and route, not just the component

Check crawlability after the build

Check

Rendered HTML

What good looks like
One accurate title, useful description, canonical, and main content in the page output
If it fails
Check Head export location, GraphQL data, and fallbacks
Check

Canonical URL

What good looks like
Absolute production URL for the intended page
If it fails
Check siteUrl, path normalization, and preview host configuration
Check

Sitemap

What good looks like
Contains public canonical routes that return success
If it fails
Check production build, page creation, filters, and plugin configuration
Check

JavaScript-dependent content

What good looks like
Important content and links appear in the rendered page
If it fails
Review rendering and use URL Inspection to compare Google's fetched page
  • Do not add a second SEO plugin until you have checked whether the Head API or existing components already emit the same tags.
  • If Head is composed through shared utilities, keep a single source of truth for title, description, and canonical data.
  • Use structured data only when it describes visible page content and meets Google's guidelines; test it after build output changes.
  • Submit the sitemap in Search Console if you want processing diagnostics. Submission does not guarantee crawling or indexing.

See the Gatsby Head API, Gatsby sitemap guide, JavaScript SEO basics, and Google sitemap submission.

Sources · 5

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • gatsbyjs.com
  • github.com
  • google.com
  1. 01Gatsby Head API | Gatsby Docsgatsbyjs.com · checked 2026-10-24
  2. 02Creating a Sitemap | Gatsby Docsgatsbyjs.com · checked 2026-10-24
  3. 03gatsby-plugin-sitemap README | GitHubgithub.com · checked 2026-10-24
  4. 04JavaScript SEO basics | Google Search Centraldevelopers.google.com · checked 2026-10-24
  5. 05Build and submit a sitemap | Google Search Centraldevelopers.google.com · checked 2026-10-24
Questions

Asked a lot.

3 questions people ask next, answered from the sources on this page.

01Does Gatsby have built-in SEO metadata?

Gatsby has a built-in Head export for adding elements to the document head of a page. Support was added in gatsby@4.19.0, so check the project version before using it.

02Where should I add Gatsby meta tags?

Export a named Head function from a page or page template, including templates created with createPage. Use queried data or page context for page-specific values.

03How do I generate a Gatsby sitemap?

Set siteUrl in siteMetadata, add gatsby-plugin-sitemap, and run a production build. The plugin writes sitemap-index.xml by default; check that its URLs resolve on the live site.

Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane