No need to create the content digest yourself  📄 🏷

We see a lot of this type of code when looking at source plugins:

exports.sourceNodes = async ({ actions }) => {
  const { createNode } = actions;
  const data = { hello: "world" };

  createNode({
    // More fields
    internal: {
      // More fields
      contentDigest: crypto
        .createHash(`md5`)
        .update(JSON.stringify(data))
        .digest(`hex`),
    },
  });
};

It is not technically wrong, but I prefer using createContentDigest like so:

exports.sourceNodes = async ({ actions, createContentDigest }) => {
  const { createNode } = actions;
  const data = { hello: "world" };

  createNode({
    // More fields
    internal: {
      // More fields
      contentDigest: createContentDigest(data), // 👈👈👈
    },
  });
};

internal.contentDigest is mandatory and thus part of a Minimal Viable Content Node. If this value has not changed, Gatsby uses the cached node instead of creating a new node, and for instance, onCreateNode is not called.

contentDigest: the digest for the content of this node. Helps Gatsby avoid doing extra work on data that hasn’t changed. Gatsby Docs

 
All the best,
Queen Raae

Stuck on a reef in the sharky waters around the Gatsby islands?

Book a 1-on-1 Gatsby Call with Queen Raae. Friendly advice you can put into action immediately, guaranteed!

Serious about Gatsby? Sign up for emails sent every weekday to help you get the most out of Gatsby!