Skip to content

Commit

Permalink
fix(gatsby): keep track of pages created by stateful createPages afte…
Browse files Browse the repository at this point in the history
…r 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 #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
  • Loading branch information
pieh authored and wardpeet committed Mar 20, 2019
1 parent 7605476 commit 62f0d10
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
21 changes: 1 addition & 20 deletions 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`)
Expand Down Expand Up @@ -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()

Expand All @@ -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`
) {
Expand Down
Expand Up @@ -52,6 +52,7 @@ Map {
"componentChunkName": "component---whatever-index-js",
"context": Object {},
"internalComponentName": "ComponentHi",
"isCreatedByStatefulCreatePages": undefined,
"jsonName": "hi-18e",
"matchPath": undefined,
"path": "/hi/",
Expand All @@ -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/",
Expand All @@ -82,6 +84,7 @@ Object {
"componentChunkName": "component---whatever-index-js",
"context": Object {},
"internalComponentName": "ComponentHi",
"isCreatedByStatefulCreatePages": undefined,
"jsonName": "hi-18e",
"matchPath": undefined,
"path": "/hi/",
Expand All @@ -104,6 +107,7 @@ Map {
"componentChunkName": "component---whatever-index-js",
"context": Object {},
"internalComponentName": "ComponentHi",
"isCreatedByStatefulCreatePages": undefined,
"jsonName": "hi-18e",
"matchPath": undefined,
"path": "/hi/",
Expand All @@ -124,6 +128,7 @@ Object {
"id": 123,
},
"internalComponentName": "ComponentHi",
"isCreatedByStatefulCreatePages": undefined,
"jsonName": "hi-18e",
"matchPath": undefined,
"path": "/hi/",
Expand All @@ -148,6 +153,7 @@ Map {
"id": 123,
},
"internalComponentName": "ComponentHi",
"isCreatedByStatefulCreatePages": undefined,
"jsonName": "hi-18e",
"matchPath": undefined,
"path": "/hi/",
Expand All @@ -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/",
Expand All @@ -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/",
Expand All @@ -207,6 +215,7 @@ Map {
"componentChunkName": "component---whatever-2-index-js",
"context": Object {},
"internalComponentName": "ComponentHi",
"isCreatedByStatefulCreatePages": undefined,
"jsonName": "hi-18e",
"matchPath": undefined,
"path": "/hi/",
Expand Down
3 changes: 3 additions & 0 deletions packages/gatsby/src/redux/actions.js
Expand Up @@ -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(),
Expand Down

0 comments on commit 62f0d10

Please sign in to comment.