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.
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
Add a couple of tests to packages/gatsby/utils/__tests__
Import and include the helper here: https://github.com/gatsbyjs/gatsby/blob/17b9dc2b2d3a11ff2663832a6df417af59db5944/packages/gatsby/src/utils/api-runner-node.js#L110 (see createNodeId for an existing example).
Update docs and examples where necessary. Key docs would be the tutorial, the API reference docs and the source plugin guide. Search across the repo for crypto for other places this could be updated. Here's a before and after example of the sourceNodes docs
- 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
If you're creating nodes yourself, you need to create a content digest. Let's make this easier for people.
@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 :)
Here you go :) https://github.com/gatsbyjs/gatsby/pull/8687