'How to get updated / lastmod value for static files for sitemap Gatsby

I've been using Gatsby and have been trying to create a sitemap with lastmod values for static pages (src/pages). I saw a random code snippet in which someone ran the query below within his gatsby-config.js and was able to get the date he last modified them.

allSitePage {
  nodes {
    path
    context {
      updated
    }
  }
}

I've not been able to achieve the same feat.

This is what I've tried so far. I've assumed he was using a context manager and set context within his js files and updating the value of the context manually every time he edited the files.

const Updated = React.createContext('2021-11-29')

class IndexPage extends React.Component {
  render() {
    return (
      <div>
        {/* Example */}
      </div>
    )
  }
}

/* Also tried IndexPage.contextType = Updated */
IndexPage.useContext = Updated

export default IndexPage

I've ran the query again, but have not been able to pass the value to be seen within the graphql query. This is the query I ran in the Graphql playground.

query MyQuery {
  allSitePage {
    nodes {
      id
      context {
        updated
      }
    }
  }
}

This is what my whole data structure looks like within the Graphql playground. data structure of graphql playground specifically allSitePage

How would I be able to get / set a updated value to be used in gatsby-config.js when creating a sitemap?



Solution 1:[1]

"allSitePage": {
      "nodes": [
        {
          "path": "/signup/united-states/new-york/"
        },
        {
          "path": "/signup/united-kingdom/london/"
        }
      ]
}

If your project have nested page structure than use this below code.

{
  resolve: "gatsby-source-filesystem",
  options: {
    name: "pages",
    path: "./src/pages/",
  },
},
`gatsby-transformer-gitinfo`, {
  resolve: "gatsby-plugin-sitemap",
  options: {
    query: `{
      site {
        siteMetadata {
          siteUrl
        }
      }
      allSitePage {
        nodes {
          path
        }
      }
      allFile(filter: {sourceInstanceName: {eq: "pages"}}) {
        edges {
          node {
            fields {
              gitLogLatestDate
            }
            relativePath
          }
        }
      }
    }`,
    resolvePages: ({
      allSitePage: {
        nodes: sitePages
      },
      allFile: {
        edges: pageFiles
      }
    }) => {
      return sitePages.map(page => {
        const pageFile = pageFiles.find(({
          node
        }) => {
          let fileName = node.relativePath.split('.').slice(0, -1).join('.')
          fileName = fileName === 'index' ? '/' : `/${fileName.replace('/index','')}/`
          return page.path === fileName;
        });

        return { ...page, ...pageFile?.node?.fields }
      })
    },
    serialize: ({
      path,
      gitLogLatestDate
    }) => {
      return {
        url: path,
        lastmod: gitLogLatestDate
      }
    },
    createLinkInHead: true,
  },
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1