node.jshttp-status-code-404gatsbysitemapnetlify

Gatsby's plugin generated sitemap.xml returning 404 error


Explanation

I'm using gatsby-plugin-sitemap to generate sitemap.xml for a Gatsby site, but, for some reason that I can't find, it's returning 404 error whenever I try to access it.

Example

I created this testing repo and hosted on Netlify (as the original is), to help debugging.

URL: https://sitemap-test1.netlify.app/
sitemap.xml: https://sitemap-test1.netlify.app/sitemap.xml (returning 404)

Thanks in advance,
Luiz.


Solution

  • 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,
              }
            },
          },
        },
      ],
    }