From d1a588d01b87540a4f2209bd5f934430f0e1182d Mon Sep 17 00:00:00 2001 From: Steven Surgnier Date: Mon, 29 Jan 2018 14:53:09 -0500 Subject: [PATCH 1/4] use createPage singleton in dev-404-page closes #3551 --- .../internal-plugins/dev-404-page/gatsby-node.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/gatsby/src/internal-plugins/dev-404-page/gatsby-node.js b/packages/gatsby/src/internal-plugins/dev-404-page/gatsby-node.js index 8540086d2d3ca..7a381d26b228d 100644 --- a/packages/gatsby/src/internal-plugins/dev-404-page/gatsby-node.js +++ b/packages/gatsby/src/internal-plugins/dev-404-page/gatsby-node.js @@ -1,6 +1,8 @@ const path = require(`path`) const fs = require(`fs-extra`) +let createdPage = false + exports.createPages = async ({ store, boundActionCreators }) => { if (process.env.NODE_ENV !== `production`) { const { program } = store.getState() @@ -8,11 +10,15 @@ exports.createPages = async ({ store, boundActionCreators }) => { const currentPath = path.join(__dirname, `./raw_dev-404-page.js`) const newPath = path.join(program.directory, `.cache`, `dev-404-page.js`) - await fs.copy(currentPath, newPath) + if (!createdPage) { + createdPage = true + + await fs.copy(currentPath, newPath) - createPage({ - component: newPath, - path: `/dev-404-page/`, - }) + createPage({ + component: newPath, + path: `/dev-404-page/`, + }) + } } } From 12328d8943c2ca0fb48b922f200d9341d64943a9 Mon Sep 17 00:00:00 2001 From: Steven Surgnier Date: Mon, 29 Jan 2018 15:41:28 -0500 Subject: [PATCH 2/4] write to a temp file and rename decrease the chance of reading from an incomplete file cc #3094 #3201 --- .../query-runner/pages-writer.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/gatsby/src/internal-plugins/query-runner/pages-writer.js b/packages/gatsby/src/internal-plugins/query-runner/pages-writer.js index f11ac7f746095..c2db003cb2967 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/pages-writer.js +++ b/packages/gatsby/src/internal-plugins/query-runner/pages-writer.js @@ -122,16 +122,17 @@ const preferDefault = m => m && m.default || m .join(`,\n`)} }` + const writeAndMove = (file, data) => { + const destination = joinPath(program.directory, `.cache`, file) + const tmp = `${destination}.${Date.now()}` + return fs.writeFile(tmp, data) + .then(() => fs.move(tmp, destination, { overwrite: true })) + } + await Promise.all([ - fs.writeFile( - joinPath(program.directory, `.cache/pages.json`), - JSON.stringify(pagesData, null, 4) - ), - fs.writeFile(`${program.directory}/.cache/sync-requires.js`, syncRequires), - fs.writeFile( - joinPath(program.directory, `.cache/async-requires.js`), - asyncRequires - ), + writeAndMove(`pages.json`, JSON.stringify(pagesData, null, 4)), + writeAndMove(`sync-requires.js`, syncRequires), + writeAndMove(`async-requires.js`, asyncRequires), ]) return From f65dae35537b43e393296cc61c4a15687fb9c712 Mon Sep 17 00:00:00 2001 From: Steven Surgnier Date: Tue, 30 Jan 2018 11:19:20 -0500 Subject: [PATCH 3/4] change dev-404-page to implement createPagesStatefully --- .../dev-404-page/gatsby-node.js | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/packages/gatsby/src/internal-plugins/dev-404-page/gatsby-node.js b/packages/gatsby/src/internal-plugins/dev-404-page/gatsby-node.js index 7a381d26b228d..8860d46051735 100644 --- a/packages/gatsby/src/internal-plugins/dev-404-page/gatsby-node.js +++ b/packages/gatsby/src/internal-plugins/dev-404-page/gatsby-node.js @@ -1,24 +1,21 @@ const path = require(`path`) const fs = require(`fs-extra`) +const chokidar = require(`chokidar`) -let createdPage = false - -exports.createPages = async ({ store, boundActionCreators }) => { +exports.createPagesStatefully = async ({ store, boundActionCreators }, options, done) => { if (process.env.NODE_ENV !== `production`) { const { program } = store.getState() const { createPage } = boundActionCreators - const currentPath = path.join(__dirname, `./raw_dev-404-page.js`) - const newPath = path.join(program.directory, `.cache`, `dev-404-page.js`) - - if (!createdPage) { - createdPage = true - - await fs.copy(currentPath, newPath) - - createPage({ - component: newPath, - path: `/dev-404-page/`, - }) - } + const source = path.join(__dirname, `./raw_dev-404-page.js`) + const destination = path.join(program.directory, `.cache`, `dev-404-page.js`) + const copy = () => fs.copy(source, destination) + await copy() + createPage({ + component: destination, + path: `/dev-404-page/`, + }) + chokidar.watch(source) + .on(`change`, () => copy()) + .on(`ready`, () => done()) } } From 4825faba87b1858b9c3ef8b549198252d0383917 Mon Sep 17 00:00:00 2001 From: Steven Surgnier Date: Tue, 30 Jan 2018 22:30:26 -0500 Subject: [PATCH 4/4] update the jest snapshot --- .../bootstrap/__tests__/__snapshots__/load-plugins.js.snap | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/bootstrap/__tests__/__snapshots__/load-plugins.js.snap b/packages/gatsby/src/bootstrap/__tests__/__snapshots__/load-plugins.js.snap index 5b87a2422b84b..06aff2ffc9522 100644 --- a/packages/gatsby/src/bootstrap/__tests__/__snapshots__/load-plugins.js.snap +++ b/packages/gatsby/src/bootstrap/__tests__/__snapshots__/load-plugins.js.snap @@ -6,7 +6,7 @@ Array [ "id": "Plugin dev-404-page", "name": "dev-404-page", "nodeAPIs": Array [ - "createPages", + "createPagesStatefully", ], "pluginOptions": Object { "plugins": Array [], @@ -103,7 +103,7 @@ Array [ "id": "Plugin dev-404-page", "name": "dev-404-page", "nodeAPIs": Array [ - "createPages", + "createPagesStatefully", ], "pluginOptions": Object { "plugins": Array [],