0

我知道我可以用对象中的exclude数组排除 url ,但是有没有办法要求所有 URL 都有一些字符串?主要是要求添加到站点地图的网址包含或optionsgatsby-plugin-sitemapen-uses-us

 {
    resolve: `gatsby-plugin-sitemap`,
    options: {
      exclude: [`all urls must contain "en-us" or "es-us"`],
    },
  },

谢谢!

更新!! 感谢@MarkoCen,我能够完成这项工作。我只是使用正则表达式在我的站点地图中添加了我想要的 URL。这是代码,如果有人感兴趣。请注意,我必须使用这种方法手动排除:/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;
      },
    },
  },
4

1 回答 1

2

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;
      }
    },
},
于 2020-01-17T18:12:16.090 回答