Skip to content

Commit

Permalink
fix(gatsby): reevaluate page query if context modified via createPage (
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy authored and sidharthachatterjee committed Aug 8, 2019
1 parent 0c85897 commit ddddc68
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
Expand Up @@ -68,4 +68,12 @@ describe(`bootstrap`, () => {
await sleep()
expect(runQueuedQueries).toBeCalledWith(path)
})

it(`will queue query when page context is changed`, async () => {
const service = getService({ isInBootstrap: false })
service.send({ type: `PAGE_CONTEXT_MODIFIED`, path: `/a/test.md` })
// there is setTimeout in action handler for `CONTEXT_CHANGES`
await sleep()
expect(enqueueExtractedQueryId).toBeCalledWith(`/a/test.md`)
})
})
11 changes: 11 additions & 0 deletions packages/gatsby/src/redux/machines/page-component.js
Expand Up @@ -22,6 +22,9 @@ module.exports = Machine(
NEW_PAGE_CREATED: {
actions: `setPage`,
},
PAGE_CONTEXT_MODIFIED: {
actions: `rerunPageQuery`,
},
QUERY_EXTRACTION_GRAPHQL_ERROR: `queryExtractionGraphQLError`,
QUERY_EXTRACTION_BABEL_ERROR: `queryExtractionBabelError`,
},
Expand Down Expand Up @@ -76,6 +79,14 @@ module.exports = Machine(
isNotBootstrapping: context => !context.isInBootstrap,
},
actions: {
rerunPageQuery: (_ctx, event) => {
const queryUtil = require(`../../query`)
// Wait a bit as calling this function immediately triggers
// an Action call which Redux squawks about.
setTimeout(() => {
queryUtil.enqueueExtractedQueryId(event.path)
}, 0)
},
runPageComponentQueries: (context, event) => {
const queryUtil = require(`../../query`)
// Wait a bit as calling this function immediately triggers
Expand Down
5 changes: 5 additions & 0 deletions packages/gatsby/src/redux/reducers/components.js
Expand Up @@ -40,6 +40,11 @@ module.exports = (state = new Map(), action) => {
service = services.get(action.payload.componentPath)
if (!service.state.context.pages.has(action.payload.path)) {
service.send({ type: `NEW_PAGE_CREATED`, path: action.payload.path })
} else if (action.contextModified) {
service.send({
type: `PAGE_CONTEXT_MODIFIED`,
path: action.payload.path,
})
}
}

Expand Down

0 comments on commit ddddc68

Please sign in to comment.