我正在使用 gatsby-plugin-sitemap 为 Gatsby 站点生成
sitemap.xml
,但是,由于某种我找不到的原因,每当我尝试访问它时,它都会返回 404 错误。
我创建了这个测试存储库并托管在 Netlify 上(与原始版本一样),以帮助调试。
网址: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 标头并在其中搜索站点地图路径。
该用户在本主题中提供了解决方案Gatsby js 中的站点地图生成问题
我已经测试过它并且有效。
将 gatsby 插件站点地图版本降级至 3.3.0。作为参考,这是我在 package.json 中的依赖项配置:
"gatsby": "5.1.0",
"gatsby-plugin-sitemap": "3.3.0",
"gatsby-plugin-robots-txt": "^1.7.1",
希望这有帮助!