From 7f510ccca4a133f518f2d24094377c24cf68d832 Mon Sep 17 00:00:00 2001 From: Kyle Mathews Date: Wed, 14 Feb 2018 16:44:06 -0800 Subject: [PATCH] Remove component query from store when a user deletes it fixes #4032 (#4041) --- .../query-runner/query-watcher.js | 24 +++++++++++++++---- .../gatsby/src/redux/reducers/components.js | 1 + 2 files changed, 21 insertions(+), 4 deletions(-) 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 174184a3797f8..de68508c9e1cf 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js +++ b/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js @@ -104,12 +104,28 @@ const watch = rootDir => { const debounceCompile = _.debounce(() => { queryCompiler().then(queries => { const components = store.getState().components + + // If a component previously with a query now doesn't — update the + // store. + const noQueryComponents = Object.values(components).filter( + c => c.query !== `` && !queries.has(c.componentPath) + ) + noQueryComponents.forEach(({ componentPath }) => { + boundActionCreators.replaceComponentQuery({ + query: ``, + componentPath, + }) + runQueriesForComponent(componentPath) + }) + + // Update the store with the new queries and re-run queries that were + // changed. queries.forEach(({ text }, id) => { - // Queries can be parsed from non page/layout components e.g. components - // with fragments so ignore those. + // Queries can be parsed from non page/layout components + // e.g. components with fragments so ignore those. // - // If the query has changed, set the new query in the store and run - // its queries. + // If the query has changed, set the new query in the + // store and run its queries. if (components[id] && text !== components[id].query) { boundActionCreators.replaceComponentQuery({ query: text, diff --git a/packages/gatsby/src/redux/reducers/components.js b/packages/gatsby/src/redux/reducers/components.js index 5b81aea4fd411..2785a82af8861 100644 --- a/packages/gatsby/src/redux/reducers/components.js +++ b/packages/gatsby/src/redux/reducers/components.js @@ -13,6 +13,7 @@ module.exports = (state = {}, action) => { state[action.payload.componentPath], { componentPath: action.payload.componentPath, + query: ``, } ) return state