Skip to content

Commit

Permalink
fix: check for dirty pages when nodes are deleted (so queries are ru-…
Browse files Browse the repository at this point in the history
…run and data is removed from pages) (#11831)

* fix: check for dirty pages when nodes are deleted (so queries are ru-run and data is removed from pages)

* typo fix

* Use fake API call as suggested by @pieh

* better name

* Fix comparision as page.updatedAt is a unix timestamp

* Remove page when deleted from internal query running cache so it can be run again if page recreated

* WIP

* MORE WIP

* Revert "MORE WIP"

This reverts commit 890fb36.

* Revert "WIP"

This reverts commit 190625f.

* Document FAKE_API_CALL
  • Loading branch information
KyleAMathews committed Feb 20, 2019
1 parent 365b18c commit 1fff689
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/gatsby/src/bootstrap/page-hot-reloader.js
Expand Up @@ -16,6 +16,11 @@ emitter.on(`CREATE_NODE`, action => {
emitter.on(`DELETE_NODE`, action => {
if (action.payload.internal.type !== `SitePage`) {
pagesDirty = true
// Make a fake API call to trigger `API_RUNNING_QUEUE_EMPTY` being called.
// We don't want to call runCreatePages here as there might be work in
// progress. So this is a safe way to make sure runCreatePages gets called
// at a safe time.
apiRunnerNode(`FAKE_API_CALL`)
}
})

Expand Down Expand Up @@ -45,7 +50,7 @@ const runCreatePages = async () => {
})
.map(p => p.id)

const timestamp = new Date().toJSON()
const timestamp = Date.now()

await apiRunnerNode(`createPages`, {
graphql,
Expand Down
Expand Up @@ -93,6 +93,15 @@ exports.runQueuedActions = runQueuedActions
emitter.on(`API_RUNNING_QUEUE_EMPTY`, runQueuedActions)

let seenIdsWithoutDataDependencies = []

// Remove pages from seenIdsWithoutDataDependencies when they're deleted
// so their query will be run again if they're created again.
emitter.on(`DELETE_PAGE`, action => {
seenIdsWithoutDataDependencies = seenIdsWithoutDataDependencies.filter(
p => p !== action.payload.path
)
})

const findIdsWithoutDataDependencies = () => {
const state = store.getState()
const allTrackedIds = _.uniq(
Expand Down
6 changes: 5 additions & 1 deletion packages/gatsby/src/utils/api-runner-node.js
Expand Up @@ -201,7 +201,11 @@ module.exports = async (api, args = {}, pluginSource) =>
})

// Check that the API is documented.
if (!apiList[api]) {
// "FAKE_API_CALL" is used when code needs to trigger something
// to happen once the the API queue is empty. Ideally of course
// we'd have an API (returning a promise) for that. But this
// works nicely in the meantime.
if (!apiList[api] && api !== `FAKE_API_CALL`) {
reporter.panic(`api: "${api}" is not a valid Gatsby api`)
}

Expand Down

0 comments on commit 1fff689

Please sign in to comment.