【问题标题】:Gatsby sitemap plugin - only include certain URLs?Gatsby 站点地图插件 - 仅包含某些 URL?
【发布时间】:2020-01-17 01:24:34
【问题描述】:

我知道我可以在options 对象中使用exclude 数组排除带有gatsby-plugin-sitemap 的网址,但是有没有办法要求所有网址都有一些字符串?主要是要求添加到站点地图的网址包含en-uses-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;
      },
    },
  },

【问题讨论】:

    标签: gatsby sitemap.xml


    【解决方案1】:

    您可以使用 queryserialize 选项从头开始构建站点地图

    {
        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;
          }
        },
    },
    

    【讨论】:

    • 谢谢,这对我们来说绝对是正确的方法。我使用了您的想法并对其进行了一些调整。直到 glob 模式具有包容性,所以我尝试“排除不包含 en-us 或 es-us 的路径”可能是不可能的。
    猜你喜欢
    • 2020-05-22
    • 2019-03-05
    • 1970-01-01
    • 2018-02-22
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 2021-10-29
    相关资源
    最近更新 更多