Notes

Dates in Gatsby GraphQL

Edit on GitHub

Gatsby
2 minutes

tl;dr: pass a Momentjs date string as the value for formatString parameter for date

1date(formatString: 'YYYY, MMM DD')
 1query BlogQuery {
 2  allFile(
 3    filter: { sourceInstanceName: { in: ["blog", "notes"] } }
 4    sort: { order: DESC, fields: childMdx___frontmatter___date }
 5  ) {
 6    nodes {
 7      id
 8      name # filename
 9      base # filename.ext
10      absolutePath # the file path
11      dir # absolutePath minus base
12      childMdx {
13        frontmatter {
14          title
15          path # the URL path
16          description
17          date(formatString: "YYYY, MMM DD") # 2020, May 03
18        }
19      }
20    }
21  }
22}

Automatically figuring out lastmod

I thought i could use the mtime value to automatically figure out the lastmod value that i usually put in frontmatter when i update an existing post. Just in case i forget to update lastmod, it’d use mtime to figure out the date the file was modified on the system.

In theory, it sounds like it may work. But practically, it doens’t. Because mtime (modified) depends on ctime (created). Your mtime value by default is equal to ctime, i.e. first modification time is when the file was created.

So if i clone the repo on May 3rd on another system, all mtime values will change to reflect May 3rd, as that’s when the file started existing on the system. And i have multiple systems where i keep cloning things..

Conclusion: You can’t use mtime to automatically generate the time the post was last modified, it’s unrelaible