Gatsby 站点地图插件 - 只包含某些 URL?
Gatsby sitemap plugin - only include certain URLs?
我知道我可以使用 gatsby-plugin-sitemap
在 options
对象中排除带有 exclude
数组的网址,但是有没有办法要求所有网址都有一些字符串?主要是希望添加到站点地图的网址包含 en-us
或 es-us
{
resolve: `gatsby-plugin-sitemap`,
options: {
exclude: [`all urls must contain "en-us" or "es-us"`],
},
},
谢谢!
更新!!
多亏了@MarkoCen,我才能让它工作。我只是使用正则表达式在站点地图中添加了我想要的 URL。如果有人感兴趣,这是代码。请注意,我必须 manually exclude /404
使用这种方法:
{
resolve: `gatsby-plugin-sitemap`,
options: {
query: `
{
site {
siteMetadata {
siteUrl
}
}
allSitePage {
edges {
node {
path
}
}
}
}`,
serialize: ({ site, allSitePage }) => {
const links = [];
for (let i = 0; i < allSitePage.edges.length; i++) {
const { path } = allSitePage.edges[i].node;
if (
/products|404|unsupported|account/.test(path)
) {
continue;
} else if (/en-us|es-us/.test(path)) {
links.push({
url: site.siteMetadata.siteUrl + path,
changefreq: 'daily',
priority: 0.8,
});
}
}
return links;
},
},
},
您可以使用 query
和 serialize
选项从头开始构建站点地图
{
resolve: `gatsby-plugin-sitemap`,
options: {
output: `/sitemap.xml`,
// use this query to fetch all the data needed for sitemap links
query: `
{
site {
siteMetadata {
siteUrl
}
blogs {
title
}
...
}
`,
serialize: ({ site, blogs }) => {
const links = [];
blogs.forEach(blog => {
// add link with en-us prefix
links.push({
url: `${site.siteMetadata.siteUrl}/en-us/blog/${blog.title}`,
changefreq: 'daily',
priority: 0.8,
});
// add link with es-us prefix
links.push({
url: `${site.siteMetadata.siteUrl}/es-us/blog/${blog.title}`
changefreq: 'daily',
priority: 0.8,
});
})
// plugin will use returned links to generate sitemap, so only include the links you want to show!
return links;
}
},
},
我知道我可以使用 gatsby-plugin-sitemap
在 options
对象中排除带有 exclude
数组的网址,但是有没有办法要求所有网址都有一些字符串?主要是希望添加到站点地图的网址包含 en-us
或 es-us
{
resolve: `gatsby-plugin-sitemap`,
options: {
exclude: [`all urls must contain "en-us" or "es-us"`],
},
},
谢谢!
更新!!
多亏了@MarkoCen,我才能让它工作。我只是使用正则表达式在站点地图中添加了我想要的 URL。如果有人感兴趣,这是代码。请注意,我必须 manually exclude /404
使用这种方法:
{
resolve: `gatsby-plugin-sitemap`,
options: {
query: `
{
site {
siteMetadata {
siteUrl
}
}
allSitePage {
edges {
node {
path
}
}
}
}`,
serialize: ({ site, allSitePage }) => {
const links = [];
for (let i = 0; i < allSitePage.edges.length; i++) {
const { path } = allSitePage.edges[i].node;
if (
/products|404|unsupported|account/.test(path)
) {
continue;
} else if (/en-us|es-us/.test(path)) {
links.push({
url: site.siteMetadata.siteUrl + path,
changefreq: 'daily',
priority: 0.8,
});
}
}
return links;
},
},
},
您可以使用 query
和 serialize
选项从头开始构建站点地图
{
resolve: `gatsby-plugin-sitemap`,
options: {
output: `/sitemap.xml`,
// use this query to fetch all the data needed for sitemap links
query: `
{
site {
siteMetadata {
siteUrl
}
blogs {
title
}
...
}
`,
serialize: ({ site, blogs }) => {
const links = [];
blogs.forEach(blog => {
// add link with en-us prefix
links.push({
url: `${site.siteMetadata.siteUrl}/en-us/blog/${blog.title}`,
changefreq: 'daily',
priority: 0.8,
});
// add link with es-us prefix
links.push({
url: `${site.siteMetadata.siteUrl}/es-us/blog/${blog.title}`
changefreq: 'daily',
priority: 0.8,
});
})
// plugin will use returned links to generate sitemap, so only include the links you want to show!
return links;
}
},
},