From a89496e23d52298d751fb5f857d6bc6f114f6936 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 19 Apr 2017 09:02:24 -0700 Subject: [PATCH 1/4] Fix id generation for JSON/YAML nodes + more tests --- .../src/gatsby-node.js | 5 +++- .../__snapshots__/gatsby-node.js.snap | 4 +-- .../__tests__/gatsby-node.js | 27 ++++++++++++++++++- .../src/gatsby-node.js | 4 +-- .../__snapshots__/gatsby-node.js.snap | 8 +++--- .../__tests__/gatsby-node.js | 27 ++++++++++++++++++- .../src/gatsby-node.js | 4 +-- 7 files changed, 66 insertions(+), 13 deletions(-) diff --git a/packages/gatsby-source-filesystem/src/gatsby-node.js b/packages/gatsby-source-filesystem/src/gatsby-node.js index 74f84ed4f5849..4c4c7961c077d 100644 --- a/packages/gatsby-source-filesystem/src/gatsby-node.js +++ b/packages/gatsby-source-filesystem/src/gatsby-node.js @@ -17,7 +17,10 @@ function readFile(file, pluginOptions, cb) { // Stringify date objects. const newFile = JSON.parse( JSON.stringify({ - id: `${slashedFile.absolutePath}`, + // Don't actually make the File id the absolute path as otherwise + // people will use the id for that and ids shouldn't be treated as + // useful information. + id: `${slashedFile.absolutePath} absPath of file`, contentDigest: contentDigest, children: [], parent: `___SOURCE___`, diff --git a/packages/gatsby-transformer-json/__tests__/__snapshots__/gatsby-node.js.snap b/packages/gatsby-transformer-json/__tests__/__snapshots__/gatsby-node.js.snap index e33630e04eb8a..d07456c913a4a 100644 --- a/packages/gatsby-transformer-json/__tests__/__snapshots__/gatsby-node.js.snap +++ b/packages/gatsby-transformer-json/__tests__/__snapshots__/gatsby-node.js.snap @@ -22,7 +22,7 @@ Array [ "content": "{\\"blue\\":false,\\"funny\\":\\"nope\\"}", "contentDigest": "f624311d932d73dcd416d2a8bea2b67d", "funny": "nope", - "id": "f624311d932d73dcd416d2a8bea2b67d", + "id": "whatever [1] >>> JSON", "mediaType": "application/json", "parent": "whatever", "type": "Test", @@ -37,7 +37,7 @@ Array [ Object { "children": Array [ "foo", - "f624311d932d73dcd416d2a8bea2b67d", + "whatever [1] >>> JSON", ], "content": "[{\\"id\\":\\"foo\\",\\"blue\\":true,\\"funny\\":\\"yup\\"},{\\"blue\\":false,\\"funny\\":\\"nope\\"}]", "contentDigest": "whatever", diff --git a/packages/gatsby-transformer-json/__tests__/gatsby-node.js b/packages/gatsby-transformer-json/__tests__/gatsby-node.js index fe3b76c8c8c8f..76ded9a667c23 100644 --- a/packages/gatsby-transformer-json/__tests__/gatsby-node.js +++ b/packages/gatsby-transformer-json/__tests__/gatsby-node.js @@ -1,4 +1,5 @@ const Promise = require("bluebird") +const _ = require("lodash") const { onNodeCreate } = require("../src/gatsby-node") @@ -39,7 +40,7 @@ describe(`Process JSON nodes correctly`, () => { }) }) - it(`If the object has an id, it uses that as the id instead of the contentDigest`, async () => { + it(`If the object has an id, it uses that as the id instead of an autogenerated one`, async () => { const data = [ { id: "foo", blue: true, funny: "yup" }, { blue: false, funny: "nope" }, @@ -58,4 +59,28 @@ describe(`Process JSON nodes correctly`, () => { expect(createNode.mock.calls[0][0].id).toEqual("foo") }) }) + + it(`the different objects shouldn't get the same ID even if they have the same content`, async () => { + const data = [ + { id: "foo", blue: true, funny: "yup" }, + { blue: false, funny: "nope" }, + { blue: false, funny: "nope" }, + { green: false, funny: "nope" }, + ] + node.content = JSON.stringify(data) + + const createNode = jest.fn() + const updateNode = jest.fn() + const boundActionCreators = { createNode, updateNode } + + await onNodeCreate({ + node, + loadNodeContent, + boundActionCreators, + }).then(() => { + const ids = createNode.mock.calls.map(object => object[0].id) + // Test that they're unique + expect(_.uniq(ids).length).toEqual(4) + }) + }) }) diff --git a/packages/gatsby-transformer-json/src/gatsby-node.js b/packages/gatsby-transformer-json/src/gatsby-node.js index fbda5f41456f7..04372269bcf6f 100644 --- a/packages/gatsby-transformer-json/src/gatsby-node.js +++ b/packages/gatsby-transformer-json/src/gatsby-node.js @@ -21,13 +21,13 @@ async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) { } const content = await loadNodeContent(node) - const JSONArray = JSON.parse(content).map(obj => { + const JSONArray = JSON.parse(content).map((obj, i) => { const objStr = JSON.stringify(obj) const contentDigest = crypto.createHash("md5").update(objStr).digest("hex") return { ...obj, - id: obj.id ? obj.id : contentDigest, + id: obj.id ? obj.id : `${node.id} [${i}] >>> JSON`, contentDigest, mediaType: `application/json`, parent: node.id, diff --git a/packages/gatsby-transformer-yaml/__tests__/__snapshots__/gatsby-node.js.snap b/packages/gatsby-transformer-yaml/__tests__/__snapshots__/gatsby-node.js.snap index fe9336a8560d5..c6e58750ac742 100644 --- a/packages/gatsby-transformer-yaml/__tests__/__snapshots__/gatsby-node.js.snap +++ b/packages/gatsby-transformer-yaml/__tests__/__snapshots__/gatsby-node.js.snap @@ -9,7 +9,7 @@ Array [ "content": "{\\"blue\\":true,\\"funny\\":\\"yup\\"}", "contentDigest": "73901821b17d5aa9dd6026181f73b64c", "funny": "yup", - "id": "73901821b17d5aa9dd6026181f73b64c", + "id": "whatever [0] >>> YAML", "mediaType": "application/json", "parent": "whatever", "type": "Test", @@ -22,7 +22,7 @@ Array [ "content": "{\\"blue\\":false,\\"funny\\":\\"nope\\"}", "contentDigest": "f624311d932d73dcd416d2a8bea2b67d", "funny": "nope", - "id": "f624311d932d73dcd416d2a8bea2b67d", + "id": "whatever [1] >>> YAML", "mediaType": "application/json", "parent": "whatever", "type": "Test", @@ -36,8 +36,8 @@ Array [ Array [ Object { "children": Array [ - "73901821b17d5aa9dd6026181f73b64c", - "f624311d932d73dcd416d2a8bea2b67d", + "whatever [0] >>> YAML", + "whatever [1] >>> YAML", ], "content": "- blue: true funny: yup diff --git a/packages/gatsby-transformer-yaml/__tests__/gatsby-node.js b/packages/gatsby-transformer-yaml/__tests__/gatsby-node.js index de5c60c26287e..5d0a3846bc54e 100644 --- a/packages/gatsby-transformer-yaml/__tests__/gatsby-node.js +++ b/packages/gatsby-transformer-yaml/__tests__/gatsby-node.js @@ -1,5 +1,6 @@ const Promise = require("bluebird") const yaml = require("js-yaml") +const _ = require("lodash") const { onNodeCreate } = require("../src/gatsby-node") @@ -37,7 +38,7 @@ describe(`Process YAML nodes correctly`, () => { }) }) - it(`If the object has an id, it uses that as the id instead of the contentDigest`, async () => { + it(`If the object has an id, it uses that as the id instead of the auto-generated one`, async () => { const data = [ { id: "foo", blue: true, funny: "yup" }, { blue: false, funny: "nope" }, @@ -56,4 +57,28 @@ describe(`Process YAML nodes correctly`, () => { expect(createNode.mock.calls[0][0].id).toEqual("foo") }) }) + + it(`the different objects shouldn't get the same ID even if they have the same content`, async () => { + const data = [ + { id: "foo", blue: true, funny: "yup" }, + { blue: false, funny: "nope" }, + { blue: false, funny: "nope" }, + { green: false, funny: "nope" }, + ] + node.content = yaml.safeDump(data) + + const createNode = jest.fn() + const updateNode = jest.fn() + const boundActionCreators = { createNode, updateNode } + + await onNodeCreate({ + node, + loadNodeContent, + boundActionCreators, + }).then(() => { + const ids = createNode.mock.calls.map(object => object[0].id) + // Test that they're unique + expect(_.uniq(ids).length).toEqual(4) + }) + }) }) diff --git a/packages/gatsby-transformer-yaml/src/gatsby-node.js b/packages/gatsby-transformer-yaml/src/gatsby-node.js index 5a6d4c19d04c6..989cf1bcf026e 100644 --- a/packages/gatsby-transformer-yaml/src/gatsby-node.js +++ b/packages/gatsby-transformer-yaml/src/gatsby-node.js @@ -12,13 +12,13 @@ async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) { } const content = await loadNodeContent(node) - const yamlArray = jsYaml.load(content).map(obj => { + const yamlArray = jsYaml.load(content).map((obj, i) => { const objStr = JSON.stringify(obj) const contentDigest = crypto.createHash("md5").update(objStr).digest("hex") return { ...obj, - id: obj.id ? obj.id : contentDigest, + id: obj.id ? obj.id : `${node.id} [${i}] >>> YAML`, contentDigest, type: _.capitalize(node.name), mediaType: `application/json`, From dafe92d1ad40824b22ea8c1e667072cbe416b0a5 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 19 Apr 2017 09:42:39 -0700 Subject: [PATCH 2/4] Debugging --- packages/gatsby-transformer-remark/src/gatsby-node.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/gatsby-transformer-remark/src/gatsby-node.js b/packages/gatsby-transformer-remark/src/gatsby-node.js index a1d5019f940fb..fc570bf78a446 100644 --- a/packages/gatsby-transformer-remark/src/gatsby-node.js +++ b/packages/gatsby-transformer-remark/src/gatsby-node.js @@ -26,6 +26,7 @@ async function onNodeCreate({ return } + console.log("loadNodeContent", loadNodeContent) const content = await loadNodeContent(node) const data = grayMatter(content) const contentDigest = crypto From f7d379aa018556215ad1a13b4f69fd1933d13a55 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 19 Apr 2017 09:55:57 -0700 Subject: [PATCH 3/4] Remove debug --- packages/gatsby-transformer-remark/src/gatsby-node.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/gatsby-transformer-remark/src/gatsby-node.js b/packages/gatsby-transformer-remark/src/gatsby-node.js index fc570bf78a446..a1d5019f940fb 100644 --- a/packages/gatsby-transformer-remark/src/gatsby-node.js +++ b/packages/gatsby-transformer-remark/src/gatsby-node.js @@ -26,7 +26,6 @@ async function onNodeCreate({ return } - console.log("loadNodeContent", loadNodeContent) const content = await loadNodeContent(node) const data = grayMatter(content) const contentDigest = crypto From a8ace752f048148251ab36ed4d595be62bca37b6 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 19 Apr 2017 09:58:25 -0700 Subject: [PATCH 4/4] Disable devtools when building --- packages/gatsby/lib/redux/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/lib/redux/index.js b/packages/gatsby/lib/redux/index.js index 3c52e956e8769..14de17aafa6d6 100644 --- a/packages/gatsby/lib/redux/index.js +++ b/packages/gatsby/lib/redux/index.js @@ -27,7 +27,8 @@ const composeEnhancers = composeWithDevTools({ }) let store -if (process.env.NODE_ENV === `test`) { +// Don't try connecting to devtools server if testing or building. +if (process.env.NODE_ENV === `test` || process.env.NODE_ENV === `production`) { store = Redux.createStore( Redux.combineReducers({ ...reducers }), initialState