queen.raae.codes · a developer’s logbook
← The log
1 min read

No need to create the content digest yourself

Written in 2022, 4 years ago. Tools and advice may have moved on since.

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

Join the crew!

All the shenanigans from me and my piratical family, straight to your inbox.

Search the log

↑↓ to move · Enter to open · Esc to close All entries →