Gatsby Sitemap

Building a sitemap

Gatsby Sitemap

by John Vincent


Posted on September 22, 2019


Building a sitemap file with Gatsby is straightforward.

Gatsby Sitemap Plugin

Using the plugin gatsby-plugin-sitemap, add to gatsby-config.js

{
    resolve: `gatsby-plugin-sitemap`,
    options: {
        query: `
        {
            site {
                siteMetadata {
                    siteUrl
                }
            }

            allSitePage(sort: {fields: path}) {
                edges {
                    node {
                        path
                    }
                }
            }
    }`,
        serialize: ({ site, allSitePage }) => {
            const result = allSitePage.edges
                .filter((item) => {
                    if (item.node.path === `/thanks/`) {
                        return false;
                    }
                    if (item.node.path.startsWith(`/private/`)) {
                        return false;
                    }
                    return true;
                })
                .map((edge) => ({
                    url: site.siteMetadata.siteUrl + edge.node.path,
                    changefreq: `daily`,
                    priority: 0.7,
                }));
            return result;
        },
    },
},

The sitemap.xml file will be build only for production builds.