Skip to content

feat: update gatsby plugins to use new createContentDigest helper #8805

@DSchau

Description

@DSchau

Summary

We recently added a createContentDigest helper which is exposed to any gatsby plugin. This internalizes the unique key creation, which changes based on changing content.

We've added this to several packages, and we should now add it to any/all packages.

Basic example

Any plugin that implements the following code snippet (or close to it) can be refactored with this new helper:

const crypto = require(`crypto`)

module.exports = ({ createNode }) => {
  const obj = { whatever: true }

  // this is the key one!
  const contentDigest = crypto
  .createHash(`md5`)
  .update(JSON.stringify(obj))
  .digest(`hex`)

  createNode() // use content digest
}

can be replaced with

const { createContentDigest } = require('gatsby-core-utils');


module.exports = ({ createNode }) => {
  const obj = { whatever: true }

  // this is the key one!
  const contentDigest = createContentDigest(obj)

  createNode() // use content digest
}

Motivation

Cleaning up code and keeping it DRY 💪

Additional Info

Any plugin that implements this new API should also bump it's peerDependency on gatsby, e.g.

{
  "peerDependencies": {
    "gatsby": ">2.0.15"
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueIssue that doesn't require previous experience with Gatsby

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions