From 62f0d1098b25b168609b50e976f0763a78ed1470 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Wed, 20 Mar 2019 22:34:57 +0100 Subject: [PATCH] fix(gatsby): keep track of pages created by stateful createPages after edits (#12671) If in `gatsby-node.js` we add ```js exports.onCreatePage = ({ actions, page }) => { actions.createPage(page) } ``` Next time any node update, pages created by `gatsby-plugin-page-creator` will be deleted. This is regression introduced by fix in https://github.com/gatsbyjs/gatsby/pull/11831 (that fixed the problem of pages never getting deleted) The problem is that when we do `createPage` by plugin/site that isn't implementing `createPagesStatefully` it will be marked to be deleted. This change keeps track if original API was `createPages` or `createPagesStatefully` by using `traceId` instead of directly checking what plugin created the page in the end. Fixes #12143 --- .../gatsby/src/bootstrap/page-hot-reloader.js | 21 +------------------ .../__tests__/__snapshots__/pages.js.snap | 9 ++++++++ packages/gatsby/src/redux/actions.js | 3 +++ 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/packages/gatsby/src/bootstrap/page-hot-reloader.js b/packages/gatsby/src/bootstrap/page-hot-reloader.js index d7065045691c3..4d4f92b0a6650 100644 --- a/packages/gatsby/src/bootstrap/page-hot-reloader.js +++ b/packages/gatsby/src/bootstrap/page-hot-reloader.js @@ -1,5 +1,3 @@ -const _ = require(`lodash`) - const { emitter, store } = require(`../redux`) const apiRunnerNode = require(`../utils/api-runner-node`) const { boundActionCreators } = require(`../redux/actions`) @@ -32,23 +30,6 @@ emitter.on(`API_RUNNING_QUEUE_EMPTY`, () => { const runCreatePages = async () => { pagesDirty = false - const plugins = store.getState().plugins - // Test which plugins implement createPagesStatefully so we can - // ignore their pages. - const statefulPlugins = plugins - .filter(p => { - try { - const gatsbyNode = require(`${p.resolve}/gatsby-node`) - if (gatsbyNode.createPagesStatefully) { - return true - } else { - return false - } - } catch (e) { - return false - } - }) - .map(p => p.id) const timestamp = Date.now() @@ -61,7 +42,7 @@ const runCreatePages = async () => { // Delete pages that weren't updated when running createPages. Array.from(store.getState().pages.values()).forEach(page => { if ( - !_.includes(statefulPlugins, page.pluginCreatorId) && + !page.isCreatedByStatefulCreatePages && page.updatedAt < timestamp && page.path !== `/404.html` ) { diff --git a/packages/gatsby/src/redux/__tests__/__snapshots__/pages.js.snap b/packages/gatsby/src/redux/__tests__/__snapshots__/pages.js.snap index 1bcc60844890d..4f30f1983d9f3 100644 --- a/packages/gatsby/src/redux/__tests__/__snapshots__/pages.js.snap +++ b/packages/gatsby/src/redux/__tests__/__snapshots__/pages.js.snap @@ -52,6 +52,7 @@ Map { "componentChunkName": "component---whatever-index-js", "context": Object {}, "internalComponentName": "ComponentHi", + "isCreatedByStatefulCreatePages": undefined, "jsonName": "hi-18e", "matchPath": undefined, "path": "/hi/", @@ -64,6 +65,7 @@ Map { "componentChunkName": "component---whatever-index-js", "context": Object {}, "internalComponentName": "ComponentHiPizza", + "isCreatedByStatefulCreatePages": undefined, "jsonName": "hi-pizza-f10", "matchPath": undefined, "path": "/hi/pizza/", @@ -82,6 +84,7 @@ Object { "componentChunkName": "component---whatever-index-js", "context": Object {}, "internalComponentName": "ComponentHi", + "isCreatedByStatefulCreatePages": undefined, "jsonName": "hi-18e", "matchPath": undefined, "path": "/hi/", @@ -104,6 +107,7 @@ Map { "componentChunkName": "component---whatever-index-js", "context": Object {}, "internalComponentName": "ComponentHi", + "isCreatedByStatefulCreatePages": undefined, "jsonName": "hi-18e", "matchPath": undefined, "path": "/hi/", @@ -124,6 +128,7 @@ Object { "id": 123, }, "internalComponentName": "ComponentHi", + "isCreatedByStatefulCreatePages": undefined, "jsonName": "hi-18e", "matchPath": undefined, "path": "/hi/", @@ -148,6 +153,7 @@ Map { "id": 123, }, "internalComponentName": "ComponentHi", + "isCreatedByStatefulCreatePages": undefined, "jsonName": "hi-18e", "matchPath": undefined, "path": "/hi/", @@ -166,6 +172,7 @@ Object { "componentChunkName": "component---whatever-index-js", "context": Object {}, "internalComponentName": "ComponentHi", + "isCreatedByStatefulCreatePages": undefined, "jsonName": "hi-18e", "matchPath": "/hi-from-somewhere-else/", "path": "/hi/", @@ -188,6 +195,7 @@ Map { "componentChunkName": "component---whatever-index-js", "context": Object {}, "internalComponentName": "ComponentHi", + "isCreatedByStatefulCreatePages": undefined, "jsonName": "hi-18e", "matchPath": "/hi-from-somewhere-else/", "path": "/hi/", @@ -207,6 +215,7 @@ Map { "componentChunkName": "component---whatever-2-index-js", "context": Object {}, "internalComponentName": "ComponentHi", + "isCreatedByStatefulCreatePages": undefined, "jsonName": "hi-18e", "matchPath": undefined, "path": "/hi/", diff --git a/packages/gatsby/src/redux/actions.js b/packages/gatsby/src/redux/actions.js index 4a80c16d6c4f7..f610b5e0cc457 100644 --- a/packages/gatsby/src/redux/actions.js +++ b/packages/gatsby/src/redux/actions.js @@ -273,6 +273,9 @@ ${reservedFields.map(f => ` * "${f}"`).join(`\n`)} matchPath: page.matchPath, component: page.component, componentChunkName: generateComponentChunkName(page.component), + isCreatedByStatefulCreatePages: + actionOptions && + actionOptions.traceId === `initial-createPagesStatefully`, // Ensure the page has a context object context: page.context || {}, updatedAt: Date.now(),