Gatsby: Add createContentDigest helper

Created on 27 Sep 2018  路  4Comments  路  Source: gatsbyjs/gatsby

Summary

Add a createContentDigest helper and pass it in to the Gatsby API runner. This means people can use it when implementing Gatsby APIS in their gatsby-node.js file.

Basic example

  • Create this file packages/gatsby/utils/create-content-digest.js, and add content a bit like:
const crypto = require(`crypto`)

// docblock goes here
const createContentDigest = obj => crypto
  .createHash(`md5`)
  .update(obj)
  .digest(`hex`)


module.exports = createContentDigest
- const crypto = require(`crypto`);
- 
- exports.sourceNodes = ({ actions, createNodeId }) => {
+ exports.sourceNodes = ({ actions, createNodeId, createContentDigest }) => {
  const { createNode } = actions;
  const myData = {
    foo: `The foo field of my node`,
    bar: `Bar bah baa baw`
  }

  const nodeContent = JSON.stringify(myData)
-    const nodeContentDigest = crypto
-    .createHash(`md5`)
-    .update(nodeContent)
-    .digest(`hex`)

  const nodeMeta = {
    id: createNodeId(`my-unique-id`),
    parent: null,
    children: [],
    internal: {
        type: `MyNodeType`,
        mediaType: `text/html`,
        content: nodeContent,
-        contentDigest: nodeContentDigest
+        contentDigest: createContentDigest(nodeContent)
    }
}

  const node = Object.assign({}, myData, nodeMeta)
  const createNode(node)
}

Ref: https://github.com/gatsbyjs/gatsby/pull/8585/files#r220931791

Motivation

If you're creating nodes yourself, you need to create a content digest. Let's make this easier for people.

Hacktoberfest good first issue help wanted

All 4 comments

@m-allanson awesome write up. I'm gonna work on this in the Pair Session today at 3PM (3 hours from now) :)

Ended up not working on this, so removing myself. Anyone else who has a pair programming session, this seems like a really good one to tackle!

@m-allanson I'll work on this :)

Was this page helpful?
0 / 5 - 0 ratings