我不能为我的Gatsy网站创建一个站点地图。
即使有几个页面,插件的默认设置也只创建一个页面:
<sitemap>
<loc>https://www.thesite.nl/sitemap/sitemap-0.xml</loc>
</sitemap>如果我试图用以下方法覆盖默认设置:
query: `{
site {
siteMetadata {
siteUrl
}
}
allSitePage {
nodes {
path
}
}
}`,
serialize: ({ site, allSitePage }) =>
allSitePage.nodes
.filter(node => {
const path = node.path
console.log({ path })
// Filter out 404 pages
if (path.includes("404")) {
return false
}
// Filter out base pages that don't have a language directory
return supportedLanguages.includes(path.split("/")[1])
})
.map(node => {
return {
url: `${site.siteMetadata.siteUrl}${node.path}`,
changefreq: `weekly`,
priority: 0.7,
}
}),TypeError: Cannot read property 'nodes' of undefined的问题是,通过gatsby开发,我可以像这样查询节点并得到路径,即使这里没有定义。
我有盖茨比v3,我认为唯一可能影响的插件是"gatsby-plugin-intl": "^0.3.3",。
{
resolve: `gatsby-plugin-intl`,
options: {
// language JSON resource path
path: `${__dirname}/src/intl`,
// supported language
languages: [`nl`, `en`],
language: `nl`,
// language file path
defaultLanguage: `nl`,
// option to redirect to `/nl` when connecting `/`
redirect: false,
},
},有什么想法吗?
在@FerranBuireu建议修改查询之后,通过gatsby build && gatsby serve通过自定义选项进行构建,现在看起来是这样的,但sitemap仍然是空的:
const siteUrl = process.env.URL || `https://www.thesite.nl`
{
resolve: "gatsby-plugin-sitemap",
options: {
query: `
{
allSitePage {
nodes {
path
}
}
}
`,
resolveSiteUrl: () => siteUrl,
resolvePages: ({ allSitePage: { nodes: allPages } }) => {
return allPages.map(page => {
return { ...page }
})
},
serialize: ({ path }) => {
return {
url: path,
}
},
},
},发布于 2021-08-31 05:18:29
我认为您的问题是因为您没有设置resolveSiteUrl,而且在这个场景中,siteUrl需要出现。根据文档
siteMetadata:{ //如果您没有使用resolveSiteUrl选项,则需要设置siteUrl:
https://www.example.com,},
理想的完整配置应该是:
const siteUrl = process.env.URL || `https://fallback.net`
// In your gatsby-config.js
module.exports = {
plugins: [
{
resolve: "gatsby-plugin-sitemap",
options: {
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,
}
},
},
},
],
}调整它并调整它以适应您的查询。
我会做这样的事情:
resolveSiteUrl: () => siteUrl,
resolvePages: ({
allSitePage: { nodes: allPages },
}) => {
const sitePageNodeMap = allSitePage.reduce((acc, node) => {
const { uri } = node
acc[uri] = node
return acc
}, {})
return allPages.map(page => {
return { ...page, ...sitePageNodeMap[page.path] }
})
},
serialize: ({ path, modifiedGmt }) => {
return {
url: path,
lastmod: modifiedGmt,
}
},发布于 2021-08-31 07:13:13
在费兰·布伊鲁的精神和技术支持下,我设法挖得更深了。
在公用文件夹中,在sitemap/sitemap-0.xml下找到了sitemap,为thesite.nl/sitemap/sitemap-0.xml的所有页面提供了正确的路径
同样值得注意的是,<sitemapindex>是指向sitemap-0:https://www.sitemaps.org/protocol.html的有效元素。Google搜索控制台仍然希望在那里提交/sitemap/sitemap-0.xml。
所以看起来页面的输出大部分时间都在那里。#白痴
https://stackoverflow.com/questions/68991036
复制相似问题