Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions docs/docs/source-plugin-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,10 @@ With the setup done, move on to adding the plugin's functionality.
Create a new file called `gatsby-node.js` in your `gatsby-source-pixabay` directory, and add the following:

```js:title=gatsby-node.js
const crypto = require("crypto")
const fetch = require("node-fetch")
const queryString = require("query-string")

exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOptions) => {
const { createNode } = actions

// Gatsby adds a configOption that's not needed for this plugin, delete it
Expand All @@ -136,15 +135,14 @@ exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
What did you do by adding this code? You started by importing the dependencies that you added earlier (along with one built in dependency):

```js
const crypto = require("crypto")
const fetch = require("node-fetch")
const queryString = require("query-string")
```

Then you implemented Gatsby's [`sourceNodes` API](/docs/node-apis/#sourceNodes) which Gatsby will run as part of its bootstrap process. When Gatsby calls `sourceNodes`, it'll pass in some helper functions (`actions` and `createNodeId`) along with any config options that are provided in your project's `gatsby-config.js` file:
Then you implemented Gatsby's [`sourceNodes` API](/docs/node-apis/#sourceNodes) which Gatsby will run as part of its bootstrap process. When Gatsby calls `sourceNodes`, it'll pass in some helper functions (`actions`, `createNodeId` and `createContentDigest`) along with any config options that are provided in your project's `gatsby-config.js` file:

```js
exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOptions) => {
```

You do some initial setup:
Expand Down Expand Up @@ -211,9 +209,8 @@ Update `gatsby-node.js` in your `plugins/gatsby-source-pixabay/` directory:
```js{11-30}:title=gatsby-node.js
const fetch = require("node-fetch")
const queryString = require("query-string")
const crypto = require("crypto")

exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOptions) => {
const { createNode } = actions

// Gatsby adds a configOption that's not needed for this plugin, delete it
Expand Down Expand Up @@ -266,14 +263,13 @@ You're ready to add the final step of your plugin - converting this data into a

### Use `createNode` function

You're adding a helper function on lines 12 to 32 and processing the data into a node on lines 49 to 52:
You're adding a helper function on lines 11 to 27 and processing the data into a node on lines 44 to 47:

```js{12-32,49-52}:title=gatsby-node.js
```js{11-27,44-47}:title=gatsby-node.js
const fetch = require("node-fetch")
const queryString = require("query-string")
const crypto = require("crypto")

exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOptions) => {
const { createNode } = actions

// Gatsby adds a configOption that's not needed for this plugin, delete it
Expand All @@ -283,10 +279,6 @@ exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
const processPhoto = photo => {
const nodeId = createNodeId(`pixabay-photo-${photo.id}`)
const nodeContent = JSON.stringify(photo)
const nodeContentDigest = crypto
.createHash("md5")
.update(nodeContent)
.digest("hex")

const nodeData = Object.assign({}, photo, {
id: nodeId,
Expand All @@ -295,7 +287,7 @@ exports.sourceNodes = ({ actions, createNodeId }, configOptions) => {
internal: {
type: `PixabayPhoto`,
content: nodeContent,
contentDigest: nodeContentDigest,
contentDigest: createContentDigest(nodeContent),
},
})

Expand Down
19 changes: 19 additions & 0 deletions packages/gatsby/src/utils/__tests__/create-content-digest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const createContentDigest = require(`../create-content-digest`)

describe(`Create content digest`, () => {
it(`returns the content digest`, () => {
const content = JSON.stringify({ id: 1 })

const contentDigest = createContentDigest(content)

expect(contentDigest).toEqual(`d2ce28b9a7fd7e4407e2b0fd499b7fe4`)
})

it(`throws an error when the data argument is not a string`, () => {
const content = [{ id: 1 }]

expect(() => {
createContentDigest(content)
}).toThrow()
})
})
10 changes: 2 additions & 8 deletions packages/gatsby/src/utils/api-node-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ exports.createPagesStatefully = true
*
* See also the documentation for [`createNode`](/docs/actions/#createNode).
* @example
* const crypto = require(`crypto`)
*
* exports.sourceNodes = ({ actions, createNodeId }) => {
* exports.sourceNodes = ({ actions, createNodeId, createContentDigest }) => {
* const { createNode } = actions
*
* // Data can come from anywhere, but for now create it manually
Expand All @@ -95,10 +93,6 @@ exports.createPagesStatefully = true
* }
*
* const nodeContent = JSON.stringify(myData)
* const nodeContentDigest = crypto
* .createHash(`md5`)
* .update(nodeContent)
* .digest(`hex`)
*
* const nodeMeta = {
* id: createNodeId(`my-data-${myData.key}`),
Expand All @@ -108,7 +102,7 @@ exports.createPagesStatefully = true
* type: `MyNodeType`,
* mediaType: `text/html`,
* content: nodeContent,
* contentDigest: nodeContentDigest
* contentDigest: createContentDigest(nodeContent)
* }
* }
*
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/utils/api-runner-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const reporter = require(`gatsby-cli/lib/reporter`)
const cache = require(`./cache`)
const apiList = require(`./api-node-docs`)
const createNodeId = require(`./create-node-id`)
const createContentDigest = require(`./create-content-digest`)

// Bind action creators per plugin so we can auto-add
// metadata to actions they create.
Expand Down Expand Up @@ -107,6 +108,7 @@ const runAPI = (plugin, api, args) => {
getNodeAndSavePathDependency,
cache,
createNodeId: namespacedCreateNodeId,
createContentDigest,
tracing,
},
plugin.pluginOptions,
Expand Down
16 changes: 16 additions & 0 deletions packages/gatsby/src/utils/create-content-digest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const crypto = require(`crypto`)

/**
* createContentDigest() Encrypts a String using md5 hash of hexadecimal digest.
*
* @param {String} data - A JSON string representing the input data
*
* @return {String} - The content digest
*/
const createContentDigest = data =>
crypto
.createHash(`md5`)
.update(data)
.digest(`hex`)

module.exports = createContentDigest