Gatsby 的插件生成 sitemap.xml 返回 404 错误

Gatsby's plugin generated sitemap.xml returning 404 error

说明

我正在使用 gatsby-plugin-sitemap 为 Gatsby 网站生成 sitemap.xml,但是,出于某种我找不到的原因,每当我尝试访问它时,它都会返回 404 错误。

例子

我创建了 this testing repo 并托管在 Netlify 上(与原来一样),以帮助调试。

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

提前致谢,
路易斯

这似乎是库的一个已知错误,您可以在以下 GitHub 个线程中看到:

就是说,您可以降级到似乎没有错误的版本 3.30,并且注意使用 options.excludes 而不是 options.exclude(尾随“s”)如果您正在排除一些页面。

否则(同时)您可以将输出路径设置为 / 作为临时解决方法:

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

基于“gatsby-plugin-sitemap”版本,您可能会获得不同的站点地图路径。查看生成的文件中的 HTML headers 并在那里搜索站点地图路径。