From 6d57b16109f193e01ea964a0bbd4349bfac60025 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Sun, 17 Feb 2019 07:48:30 +0100 Subject: [PATCH 01/11] fix: check for dirty pages when nodes are deleted (so queries are ru-run and data is removed from pages) --- packages/gatsby/src/bootstrap/page-hot-reloader.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/gatsby/src/bootstrap/page-hot-reloader.js b/packages/gatsby/src/bootstrap/page-hot-reloader.js index c83e335f1084f..24f2b90a4624f 100644 --- a/packages/gatsby/src/bootstrap/page-hot-reloader.js +++ b/packages/gatsby/src/bootstrap/page-hot-reloader.js @@ -16,6 +16,11 @@ emitter.on(`CREATE_NODE`, action => { emitter.on(`DELETE_NODE`, action => { if (action.payload.internal.type !== `SitePage`) { pagesDirty = true + // We call runCreatePages directly as + // there often isn't API calls associated with deleting nodes + // (especially with stateful source plugins like gatsby-source-filesystem) + // so API_RUNNING_QUEUE_EMPTY won't be invoked. + debouncedRunCreatePages() } }) @@ -66,6 +71,7 @@ const runCreatePages = async () => { emitter.emit(`CREATE_PAGE_END`) } +const debouncedRunCreatePages = _.debounce(runCreatePages, 150) module.exports = graphqlRunner => { graphql = graphqlRunner From c3b9ec9d6cba7ac401d4706a8e01397ee848a777 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Sun, 17 Feb 2019 08:09:21 +0100 Subject: [PATCH 02/11] typo fix --- packages/gatsby/src/bootstrap/page-hot-reloader.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/bootstrap/page-hot-reloader.js b/packages/gatsby/src/bootstrap/page-hot-reloader.js index 24f2b90a4624f..7b0560435631d 100644 --- a/packages/gatsby/src/bootstrap/page-hot-reloader.js +++ b/packages/gatsby/src/bootstrap/page-hot-reloader.js @@ -20,7 +20,12 @@ emitter.on(`DELETE_NODE`, action => { // there often isn't API calls associated with deleting nodes // (especially with stateful source plugins like gatsby-source-filesystem) // so API_RUNNING_QUEUE_EMPTY won't be invoked. - debouncedRunCreatePages() + // + // This is pretty hacky and we need a smarter heuristic to decide + // whether we need to call runCreatePages() or not ourselves. + if (action.payload.internal.type === `File`) { + runCreatePages() + } } }) @@ -71,7 +76,6 @@ const runCreatePages = async () => { emitter.emit(`CREATE_PAGE_END`) } -const debouncedRunCreatePages = _.debounce(runCreatePages, 150) module.exports = graphqlRunner => { graphql = graphqlRunner From eb16fd806a694a5befa160ab6a1456901ab070b4 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Sun, 17 Feb 2019 18:41:53 +0100 Subject: [PATCH 03/11] Use fake API call as suggested by @pieh --- packages/gatsby/src/bootstrap/page-hot-reloader.js | 11 +---------- packages/gatsby/src/utils/api-runner-node.js | 2 +- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/packages/gatsby/src/bootstrap/page-hot-reloader.js b/packages/gatsby/src/bootstrap/page-hot-reloader.js index 7b0560435631d..edb32c7803277 100644 --- a/packages/gatsby/src/bootstrap/page-hot-reloader.js +++ b/packages/gatsby/src/bootstrap/page-hot-reloader.js @@ -16,16 +16,7 @@ emitter.on(`CREATE_NODE`, action => { emitter.on(`DELETE_NODE`, action => { if (action.payload.internal.type !== `SitePage`) { pagesDirty = true - // We call runCreatePages directly as - // there often isn't API calls associated with deleting nodes - // (especially with stateful source plugins like gatsby-source-filesystem) - // so API_RUNNING_QUEUE_EMPTY won't be invoked. - // - // This is pretty hacky and we need a smarter heuristic to decide - // whether we need to call runCreatePages() or not ourselves. - if (action.payload.internal.type === `File`) { - runCreatePages() - } + apiRunnerNode(`I_AM_SO_FAKE`) } }) diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index 8c2484231f50f..c1b8b0612cdf1 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -157,7 +157,7 @@ module.exports = async (api, args = {}, pluginSource) => }) // Check that the API is documented. - if (!apiList[api]) { + if (!apiList[api] && api !== `I_AM_SO_FAKE`) { reporter.panic(`api: "${api}" is not a valid Gatsby api`) } From a9d9a2ea027aa3a37c5b556a3de4a14bb7b164d4 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Sun, 17 Feb 2019 19:38:37 +0100 Subject: [PATCH 04/11] better name --- packages/gatsby/src/bootstrap/page-hot-reloader.js | 2 +- packages/gatsby/src/utils/api-runner-node.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/bootstrap/page-hot-reloader.js b/packages/gatsby/src/bootstrap/page-hot-reloader.js index edb32c7803277..b6d0060c18957 100644 --- a/packages/gatsby/src/bootstrap/page-hot-reloader.js +++ b/packages/gatsby/src/bootstrap/page-hot-reloader.js @@ -16,7 +16,7 @@ emitter.on(`CREATE_NODE`, action => { emitter.on(`DELETE_NODE`, action => { if (action.payload.internal.type !== `SitePage`) { pagesDirty = true - apiRunnerNode(`I_AM_SO_FAKE`) + apiRunnerNode(`FAKE_API_CALL`) } }) diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index c1b8b0612cdf1..7dec65b613d82 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -157,7 +157,7 @@ module.exports = async (api, args = {}, pluginSource) => }) // Check that the API is documented. - if (!apiList[api] && api !== `I_AM_SO_FAKE`) { + if (!apiList[api] && api !== `FAKE_API_CALL`) { reporter.panic(`api: "${api}" is not a valid Gatsby api`) } From 7ffe5c74d9e1fc1b47d75fae893f9b7692fd2e2a Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Mon, 18 Feb 2019 07:53:41 +0100 Subject: [PATCH 05/11] Fix comparision as page.updatedAt is a unix timestamp --- packages/gatsby/src/bootstrap/page-hot-reloader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/src/bootstrap/page-hot-reloader.js b/packages/gatsby/src/bootstrap/page-hot-reloader.js index b6d0060c18957..77cbc753f9de9 100644 --- a/packages/gatsby/src/bootstrap/page-hot-reloader.js +++ b/packages/gatsby/src/bootstrap/page-hot-reloader.js @@ -46,7 +46,7 @@ const runCreatePages = async () => { }) .map(p => p.id) - const timestamp = new Date().toJSON() + const timestamp = Date.now() await apiRunnerNode(`createPages`, { graphql, From 5b5417fafed850c17560705dc007fecf7b12a64f Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Mon, 18 Feb 2019 14:15:40 +0100 Subject: [PATCH 06/11] Remove page when deleted from internal query running cache so it can be run again if page recreated --- .../internal-plugins/query-runner/page-query-runner.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js b/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js index f273ccac1717a..40a9c781a0c55 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js +++ b/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js @@ -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( From 190625f6237553728675059822dda5736c515b43 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Mon, 18 Feb 2019 17:11:15 +0100 Subject: [PATCH 07/11] WIP --- .../query-runner/page-query-runner.js | 9 +++++++- .../query-runner/query-runner.js | 3 +++ .../query-runner/query-watcher.js | 19 +++++++++++++++-- .../gatsby/src/redux/reducers/components.js | 21 ++++++++++++++++--- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js b/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js index 40a9c781a0c55..45e39fd968d23 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js +++ b/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js @@ -155,7 +155,14 @@ const runQueriesForPathnames = pathnames => { let didNotQueueItems = true pageQueries.forEach(id => { const page = pages.get(id) - if (page) { + // Don't run queries for page components that haven't yet + // had their queries extracted. Once that is finished, the pages + // with using that component will have their queries queued again. + if ( + page && + store.getState().components.get(page.componentPath).queryState !== + `QUERY_NOT_YET_EXTRACTED` + ) { didNotQueueItems = false queue.push( ({ diff --git a/packages/gatsby/src/internal-plugins/query-runner/query-runner.js b/packages/gatsby/src/internal-plugins/query-runner/query-runner.js index 1e3197efc43f4..50fa489e4de60 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/query-runner.js +++ b/packages/gatsby/src/internal-plugins/query-runner/query-runner.js @@ -27,6 +27,8 @@ type QueryJob = { module.exports = async (queryJob: QueryJob, component: Any) => { const { schema, program } = store.getState() + console.log(`running query for`, queryJob.context.path) + console.log({ query: queryJob.query }) const graphql = (query, context) => graphqlFunction(schema, query, context, context, context) @@ -38,6 +40,7 @@ module.exports = async (queryJob: QueryJob, component: Any) => { } else { result = await graphql(queryJob.query, queryJob.context) } + console.log({ result }) // If there's a graphql error then log the error. If we're building, also // quit. diff --git a/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js b/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js index d0b21110656d0..6341d64aba2e9 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js +++ b/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js @@ -13,7 +13,7 @@ const chokidar = require(`chokidar`) const path = require(`path`) const slash = require(`slash`) -const { store, emitter } = require(`../../redux/`) +const { store } = require(`../../redux/`) const { boundActionCreators } = require(`../../redux/actions`) const queryCompiler = require(`./query-compiler`).default const report = require(`gatsby-cli/lib/reporter`) @@ -45,6 +45,7 @@ const handleComponentsWithRemovedQueries = ( debug(`Page query was removed from ${c.componentPath}`) boundActionCreators.replaceComponentQuery({ query: ``, + queryState: `QUERY_EXTRACTED`, componentPath: c.componentPath, }) queueQueriesForPageComponent(c.componentPath) @@ -65,6 +66,17 @@ const handleComponentsWithRemovedQueries = ( }) } +const handlePageComponentsWithNoQueries = ({ components }, queries) => { + components.forEach(c => { + if (c.queryState === `QUERY_NOT_YET_EXTRACTED`) { + boundActionCreators.replaceComponentQuery({ + queryState: `QUERY_EXTRACTED`, + componentPath: c.componentPath, + }) + } + }) +} + const handleQuery = ( { components, staticQueryComponents }, query, @@ -106,7 +118,7 @@ const handleQuery = ( } return true - // If this is page query + // If this is page query. } else if (components.has(component)) { if (components.get(component).query !== query.text) { boundActionCreators.replaceComponentQuery({ @@ -133,6 +145,7 @@ const updateStateAndRunQueries = isFirstRun => { const snapshot = getQueriesSnapshot() return queryCompiler().then(queries => { handleComponentsWithRemovedQueries(snapshot, queries) + handleComponentsWithNoQueries(snapshot, queries) let queriesWillNotRun = false queries.forEach((query, component) => { @@ -244,8 +257,10 @@ const watchComponent = componentPath => { !filesToWatch.has(componentPath) ) { filesToWatch.add(componentPath) + console.log(`watchComponent`, componentPath) if (watcher) { watcher.add(componentPath) + updateStateAndRunQueries() } } } diff --git a/packages/gatsby/src/redux/reducers/components.js b/packages/gatsby/src/redux/reducers/components.js index 175495081fe3c..7d4e7eacb4bee 100644 --- a/packages/gatsby/src/redux/reducers/components.js +++ b/packages/gatsby/src/redux/reducers/components.js @@ -7,19 +7,34 @@ module.exports = (state = new Map(), action) => { return new Map() case `CREATE_PAGE`: action.payload.componentPath = normalize(action.payload.component) + console.log( + `CREATE_PAGE`, + action.payload.componentPath, + action.payload.query + ) state.set( action.payload.componentPath, - _.merge({ query: `` }, state.get(action.payload.componentPath), { - componentPath: action.payload.componentPath, - }) + _.merge( + { query: ``, queryState: `QUERY_NOT_YET_EXTRACTED` }, + state.get(action.payload.componentPath), + { + componentPath: action.payload.componentPath, + } + ) ) return state case `REMOVE_TEMPLATE_COMPONENT`: state.delete(normalize(action.payload.componentPath)) return state case `REPLACE_COMPONENT_QUERY`: + console.log( + `REPLACE_COMPONENT_QUERY`, + action.payload.componentPath, + action.payload.query + ) action.payload.componentPath = normalize(action.payload.componentPath) state.set(action.payload.componentPath, { + queryState: `QUERY_EXTRACTED`, ...state.get(action.payload.componentPath), query: action.payload.query, }) From 890fb367eaae649bbe981edf4baf5cf071ce97bc Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Tue, 19 Feb 2019 00:06:07 -0800 Subject: [PATCH 08/11] MORE WIP --- .../query-runner/page-query-runner.js | 13 +++++++++---- .../internal-plugins/query-runner/query-watcher.js | 9 +++++++-- packages/gatsby/src/redux/reducers/components.js | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js b/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js index 45e39fd968d23..2a21650894c34 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js +++ b/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js @@ -17,7 +17,6 @@ const { store, emitter } = require(`../../redux`) let queuedDirtyActions = [] let active = false -let running = false const runQueriesForPathnamesQueue = new Set() exports.queueQueryForPathname = pathname => { @@ -50,6 +49,7 @@ const runQueries = async () => { ...dirtyIds, ...cleanIds, ]) + console.log({ pathnamesToRun }) runQueriesForPathnamesQueue.clear() @@ -73,12 +73,13 @@ emitter.on(`CREATE_PAGE`, action => { }) const runQueuedActions = async () => { - if (active && !running) { + console.log(`runQueuedActions`, { active }) + if (active) { try { - running = true await runQueries() } finally { - running = false + // TODO what does this mean? + // Why "finally"? if (queuedDirtyActions.length > 0) { runQueuedActions() } @@ -155,6 +156,10 @@ const runQueriesForPathnames = pathnames => { let didNotQueueItems = true pageQueries.forEach(id => { const page = pages.get(id) + console.log({ + page, + component: store.getState().components.get(page.componentPath), + }) // Don't run queries for page components that haven't yet // had their queries extracted. Once that is finished, the pages // with using that component will have their queries queued again. diff --git a/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js b/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js index 6341d64aba2e9..041ac6d8dae9b 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js +++ b/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js @@ -13,7 +13,7 @@ const chokidar = require(`chokidar`) const path = require(`path`) const slash = require(`slash`) -const { store } = require(`../../redux/`) +const { store, emitter } = require(`../../redux/`) const { boundActionCreators } = require(`../../redux/actions`) const queryCompiler = require(`./query-compiler`).default const report = require(`gatsby-cli/lib/reporter`) @@ -70,11 +70,15 @@ const handlePageComponentsWithNoQueries = ({ components }, queries) => { components.forEach(c => { if (c.queryState === `QUERY_NOT_YET_EXTRACTED`) { boundActionCreators.replaceComponentQuery({ + query: ``, queryState: `QUERY_EXTRACTED`, componentPath: c.componentPath, }) + queueQueriesForPageComponent(c.componentPath) } }) + console.log({ runQueuedQueries }) + runQueuedQueries() } const handleQuery = ( @@ -145,7 +149,7 @@ const updateStateAndRunQueries = isFirstRun => { const snapshot = getQueriesSnapshot() return queryCompiler().then(queries => { handleComponentsWithRemovedQueries(snapshot, queries) - handleComponentsWithNoQueries(snapshot, queries) + handlePageComponentsWithNoQueries(snapshot, queries) let queriesWillNotRun = false queries.forEach((query, component) => { @@ -228,6 +232,7 @@ exports.extractQueries = () => { } const queueQueriesForPageComponent = componentPath => { + console.log(`queueQueriesForPageComponent`, { componentPath }) const pages = getPagesForComponent(componentPath) // Remove page data dependencies before re-running queries because // the changing of the query could have changed the data dependencies. diff --git a/packages/gatsby/src/redux/reducers/components.js b/packages/gatsby/src/redux/reducers/components.js index 7d4e7eacb4bee..4610cf114b7ed 100644 --- a/packages/gatsby/src/redux/reducers/components.js +++ b/packages/gatsby/src/redux/reducers/components.js @@ -34,9 +34,9 @@ module.exports = (state = new Map(), action) => { ) action.payload.componentPath = normalize(action.payload.componentPath) state.set(action.payload.componentPath, { - queryState: `QUERY_EXTRACTED`, ...state.get(action.payload.componentPath), query: action.payload.query, + queryState: `QUERY_EXTRACTED`, }) return state } From dcfc73c7d0f7bc8dc33ce9ee04540c5ed94a59db Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 20 Feb 2019 02:28:30 -0800 Subject: [PATCH 09/11] Revert "MORE WIP" This reverts commit 890fb367eaae649bbe981edf4baf5cf071ce97bc. --- .../query-runner/page-query-runner.js | 13 ++++--------- .../internal-plugins/query-runner/query-watcher.js | 9 ++------- packages/gatsby/src/redux/reducers/components.js | 2 +- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js b/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js index 2a21650894c34..45e39fd968d23 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js +++ b/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js @@ -17,6 +17,7 @@ const { store, emitter } = require(`../../redux`) let queuedDirtyActions = [] let active = false +let running = false const runQueriesForPathnamesQueue = new Set() exports.queueQueryForPathname = pathname => { @@ -49,7 +50,6 @@ const runQueries = async () => { ...dirtyIds, ...cleanIds, ]) - console.log({ pathnamesToRun }) runQueriesForPathnamesQueue.clear() @@ -73,13 +73,12 @@ emitter.on(`CREATE_PAGE`, action => { }) const runQueuedActions = async () => { - console.log(`runQueuedActions`, { active }) - if (active) { + if (active && !running) { try { + running = true await runQueries() } finally { - // TODO what does this mean? - // Why "finally"? + running = false if (queuedDirtyActions.length > 0) { runQueuedActions() } @@ -156,10 +155,6 @@ const runQueriesForPathnames = pathnames => { let didNotQueueItems = true pageQueries.forEach(id => { const page = pages.get(id) - console.log({ - page, - component: store.getState().components.get(page.componentPath), - }) // Don't run queries for page components that haven't yet // had their queries extracted. Once that is finished, the pages // with using that component will have their queries queued again. diff --git a/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js b/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js index 041ac6d8dae9b..6341d64aba2e9 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js +++ b/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js @@ -13,7 +13,7 @@ const chokidar = require(`chokidar`) const path = require(`path`) const slash = require(`slash`) -const { store, emitter } = require(`../../redux/`) +const { store } = require(`../../redux/`) const { boundActionCreators } = require(`../../redux/actions`) const queryCompiler = require(`./query-compiler`).default const report = require(`gatsby-cli/lib/reporter`) @@ -70,15 +70,11 @@ const handlePageComponentsWithNoQueries = ({ components }, queries) => { components.forEach(c => { if (c.queryState === `QUERY_NOT_YET_EXTRACTED`) { boundActionCreators.replaceComponentQuery({ - query: ``, queryState: `QUERY_EXTRACTED`, componentPath: c.componentPath, }) - queueQueriesForPageComponent(c.componentPath) } }) - console.log({ runQueuedQueries }) - runQueuedQueries() } const handleQuery = ( @@ -149,7 +145,7 @@ const updateStateAndRunQueries = isFirstRun => { const snapshot = getQueriesSnapshot() return queryCompiler().then(queries => { handleComponentsWithRemovedQueries(snapshot, queries) - handlePageComponentsWithNoQueries(snapshot, queries) + handleComponentsWithNoQueries(snapshot, queries) let queriesWillNotRun = false queries.forEach((query, component) => { @@ -232,7 +228,6 @@ exports.extractQueries = () => { } const queueQueriesForPageComponent = componentPath => { - console.log(`queueQueriesForPageComponent`, { componentPath }) const pages = getPagesForComponent(componentPath) // Remove page data dependencies before re-running queries because // the changing of the query could have changed the data dependencies. diff --git a/packages/gatsby/src/redux/reducers/components.js b/packages/gatsby/src/redux/reducers/components.js index 4610cf114b7ed..7d4e7eacb4bee 100644 --- a/packages/gatsby/src/redux/reducers/components.js +++ b/packages/gatsby/src/redux/reducers/components.js @@ -34,9 +34,9 @@ module.exports = (state = new Map(), action) => { ) action.payload.componentPath = normalize(action.payload.componentPath) state.set(action.payload.componentPath, { + queryState: `QUERY_EXTRACTED`, ...state.get(action.payload.componentPath), query: action.payload.query, - queryState: `QUERY_EXTRACTED`, }) return state } From 863acf14c81b792aca18e3e1e9df5390ade3907c Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 20 Feb 2019 02:28:49 -0800 Subject: [PATCH 10/11] Revert "WIP" This reverts commit 190625f6237553728675059822dda5736c515b43. --- .../query-runner/page-query-runner.js | 9 +------- .../query-runner/query-runner.js | 3 --- .../query-runner/query-watcher.js | 19 ++--------------- .../gatsby/src/redux/reducers/components.js | 21 +++---------------- 4 files changed, 6 insertions(+), 46 deletions(-) diff --git a/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js b/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js index 45e39fd968d23..40a9c781a0c55 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js +++ b/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js @@ -155,14 +155,7 @@ const runQueriesForPathnames = pathnames => { let didNotQueueItems = true pageQueries.forEach(id => { const page = pages.get(id) - // Don't run queries for page components that haven't yet - // had their queries extracted. Once that is finished, the pages - // with using that component will have their queries queued again. - if ( - page && - store.getState().components.get(page.componentPath).queryState !== - `QUERY_NOT_YET_EXTRACTED` - ) { + if (page) { didNotQueueItems = false queue.push( ({ diff --git a/packages/gatsby/src/internal-plugins/query-runner/query-runner.js b/packages/gatsby/src/internal-plugins/query-runner/query-runner.js index 50fa489e4de60..1e3197efc43f4 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/query-runner.js +++ b/packages/gatsby/src/internal-plugins/query-runner/query-runner.js @@ -27,8 +27,6 @@ type QueryJob = { module.exports = async (queryJob: QueryJob, component: Any) => { const { schema, program } = store.getState() - console.log(`running query for`, queryJob.context.path) - console.log({ query: queryJob.query }) const graphql = (query, context) => graphqlFunction(schema, query, context, context, context) @@ -40,7 +38,6 @@ module.exports = async (queryJob: QueryJob, component: Any) => { } else { result = await graphql(queryJob.query, queryJob.context) } - console.log({ result }) // If there's a graphql error then log the error. If we're building, also // quit. diff --git a/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js b/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js index 6341d64aba2e9..d0b21110656d0 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js +++ b/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js @@ -13,7 +13,7 @@ const chokidar = require(`chokidar`) const path = require(`path`) const slash = require(`slash`) -const { store } = require(`../../redux/`) +const { store, emitter } = require(`../../redux/`) const { boundActionCreators } = require(`../../redux/actions`) const queryCompiler = require(`./query-compiler`).default const report = require(`gatsby-cli/lib/reporter`) @@ -45,7 +45,6 @@ const handleComponentsWithRemovedQueries = ( debug(`Page query was removed from ${c.componentPath}`) boundActionCreators.replaceComponentQuery({ query: ``, - queryState: `QUERY_EXTRACTED`, componentPath: c.componentPath, }) queueQueriesForPageComponent(c.componentPath) @@ -66,17 +65,6 @@ const handleComponentsWithRemovedQueries = ( }) } -const handlePageComponentsWithNoQueries = ({ components }, queries) => { - components.forEach(c => { - if (c.queryState === `QUERY_NOT_YET_EXTRACTED`) { - boundActionCreators.replaceComponentQuery({ - queryState: `QUERY_EXTRACTED`, - componentPath: c.componentPath, - }) - } - }) -} - const handleQuery = ( { components, staticQueryComponents }, query, @@ -118,7 +106,7 @@ const handleQuery = ( } return true - // If this is page query. + // If this is page query } else if (components.has(component)) { if (components.get(component).query !== query.text) { boundActionCreators.replaceComponentQuery({ @@ -145,7 +133,6 @@ const updateStateAndRunQueries = isFirstRun => { const snapshot = getQueriesSnapshot() return queryCompiler().then(queries => { handleComponentsWithRemovedQueries(snapshot, queries) - handleComponentsWithNoQueries(snapshot, queries) let queriesWillNotRun = false queries.forEach((query, component) => { @@ -257,10 +244,8 @@ const watchComponent = componentPath => { !filesToWatch.has(componentPath) ) { filesToWatch.add(componentPath) - console.log(`watchComponent`, componentPath) if (watcher) { watcher.add(componentPath) - updateStateAndRunQueries() } } } diff --git a/packages/gatsby/src/redux/reducers/components.js b/packages/gatsby/src/redux/reducers/components.js index 7d4e7eacb4bee..175495081fe3c 100644 --- a/packages/gatsby/src/redux/reducers/components.js +++ b/packages/gatsby/src/redux/reducers/components.js @@ -7,34 +7,19 @@ module.exports = (state = new Map(), action) => { return new Map() case `CREATE_PAGE`: action.payload.componentPath = normalize(action.payload.component) - console.log( - `CREATE_PAGE`, - action.payload.componentPath, - action.payload.query - ) state.set( action.payload.componentPath, - _.merge( - { query: ``, queryState: `QUERY_NOT_YET_EXTRACTED` }, - state.get(action.payload.componentPath), - { - componentPath: action.payload.componentPath, - } - ) + _.merge({ query: `` }, state.get(action.payload.componentPath), { + componentPath: action.payload.componentPath, + }) ) return state case `REMOVE_TEMPLATE_COMPONENT`: state.delete(normalize(action.payload.componentPath)) return state case `REPLACE_COMPONENT_QUERY`: - console.log( - `REPLACE_COMPONENT_QUERY`, - action.payload.componentPath, - action.payload.query - ) action.payload.componentPath = normalize(action.payload.componentPath) state.set(action.payload.componentPath, { - queryState: `QUERY_EXTRACTED`, ...state.get(action.payload.componentPath), query: action.payload.query, }) From 516b42c2a5938e1bb62193fc2ddf8adffa2df409 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 20 Feb 2019 02:58:06 -0800 Subject: [PATCH 11/11] Document FAKE_API_CALL --- packages/gatsby/src/bootstrap/page-hot-reloader.js | 4 ++++ packages/gatsby/src/utils/api-runner-node.js | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/packages/gatsby/src/bootstrap/page-hot-reloader.js b/packages/gatsby/src/bootstrap/page-hot-reloader.js index 77cbc753f9de9..6eab902e0ab97 100644 --- a/packages/gatsby/src/bootstrap/page-hot-reloader.js +++ b/packages/gatsby/src/bootstrap/page-hot-reloader.js @@ -16,6 +16,10 @@ 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`) } }) diff --git a/packages/gatsby/src/utils/api-runner-node.js b/packages/gatsby/src/utils/api-runner-node.js index 7dec65b613d82..12a4a74ad7a0a 100644 --- a/packages/gatsby/src/utils/api-runner-node.js +++ b/packages/gatsby/src/utils/api-runner-node.js @@ -157,6 +157,10 @@ module.exports = async (api, args = {}, pluginSource) => }) // Check that the API is documented. + // "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`) }