Gatsby's plugin generated sitemap.xml returning 404 error

Viewed 2200
2 Answers

It seems to be a known bug of the library, as you can see in the following GitHub threads:

That said, you can downgrade to version 3.30 which seems to be bug-free and be aware of using options.excludes instead of options.exclude (trailing "s") if you are excluding some pages.

Otherwise (and in the meantime) you can set the output path to / as a temporary workaround:

module.exports = {
  plugins: [
    {
      resolve: "gatsby-plugin-sitemap",
      options: {
        output: "/",
        query: `
        {
          allSitePage {
            nodes {
              path
            }
          }
          allWpContentNode(filter: {nodeType: {in: ["Post", "Page"]}}) {
            nodes {
              ... on WpPost {
                uri
                modifiedGmt
              }
              ... on WpPage {
                uri
                modifiedGmt
              }
            }
          }
        }
      `,
        resolveSiteUrl: () => siteUrl,
        resolvePages: ({
          allSitePage: { nodes: allPages },
          allWpContentNode: { nodes: allWpNodes },
        }) => {
          const wpNodeMap = allWpNodes.reduce((acc, node) => {
            const { uri } = node
            acc[uri] = node

            return acc
          }, {})

          return allPages.map(page => {
            return { ...page, ...wpNodeMap[page.path] }
          })
        },
        serialize: ({ path, modifiedGmt }) => {
          return {
            url: path,
            lastmod: modifiedGmt,
          }
        },
      },
    },
  ],
}

Based on the "gatsby-plugin-sitemap" version, you might get a different sitemap path. Check out the HTML headers in your generated files and search for the sitemap path there.

Related