Gatsby-plugin-sitemap,自定义配置,需要使用自定义resolvePages和Serialize来整合页面和markdown,这行代码是做什么的?

Gatsby-plugin-sitemap, custom config, need to integrate pages and markdown using custom resolvePages and Serialize, what does this line of code do?

刚从 javascript 开始,感谢 Gatsby,如果这是一个新手问题,请原谅。也只是从 Whosebug 上的 posting 开始,通常只是消费内容,很抱歉,如果我的 post 不完整或不清楚。

我正在使用 GatsbyJs 构建一个网站,并想使用 gatsby-plugin-sitemap 设置一个合适的站点地图,但是我很难理解以下代码行的作用,所以我可以尝试自定义代码来做我需要的是在站点地图上整合页面和博客 post,并在适用时添加适当的 lastmod。我快崩溃了,但无法让最后一部分工作,也就是说,当它是博客时添加 lastmod post。

on gatsby-config.js:
{
      resolve: `gatsby-plugin-sitemap`,
      options: {

        // Exclude specific pages or groups of pages using glob parameters
        // See: https://github.com/isaacs/minimatch
        // The example below will exclude the single `path/to/page` and all routes beginning with `category`
        output: '/',
        excludes: [`/legal/*`,`/gracias`],
        query: `
        {
           site {
             siteMetadata {
               siteUrl
             }
             buildTime(formatString: "YYYY-MM-DD")
           }
           allSitePage {
             nodes {
               path
             }
           }
           allMarkdownRemark(filter: {frontmatter: {path: {regex: "/blog/"}}}) {
             nodes {
               frontmatter {
                 path
                 date
               }
             }
           }
         }
        `,

        resolvePages: ({
          allSitePage: { nodes: allPages },
          allMarkdownRemark: { nodes: allPosts },
        }) => {
          const pathToDateMap = {};

          allPosts.map(post => {
            pathToDateMap [post.frontmatter.path] = { date: post.frontmatter.date };
          });

          const pages = allPages.map(page => {
            return { ...page, ...pathToDateMap [page.path] };//what does this line of code do???
          });

          return pages;
        },
        serialize: ({ path, date, buildTime }) => {
          let entry = {
            url: path,
            changefreq: 'monthly',
            priority: 0.5,
          };

          if (date) {
            entry.priority = 0.7;
            entry.lastmod = date;
          } else {

            entry.lastmod = buildtime;

          }

          return entry;
        }

      }
    }

据您所知,开发和构建均已成功,站点地图生成为站点地图索引和站点地图-0.xml,输出在那里,但没有页面上有 lastmod。

谢谢大家的帮助,

有了这个:

return { ...page, ...pathToDateMap [page.path] };

您正在使用 spread operator (...) 合并对象。因此,您返回的对象是 pagepathToDateMap(在 page.path 位置)的所有属性的结果。

例如:

let person = {
    firstName: 'John',
    lastName: 'Doe',
    age: 25,
};


let job = {
    jobTitle: 'Web developer',
    location: 'USA'
};

let employee = {
    ...person,
    ...job
};

console.log(employee)

在上面的代码片段中,employee 是合并 personjob 的结果。

默认情况下不会添加 lastmod 参数,除非您的来源有(WordPress 通常有)但您可以按照此答案 创建您自己的。