Skip to content

Commit f50093e

Browse files
authored
fix(gatsby): wait for extracted query enqueuing before running queries (#27408)
* add failing e2e test * fix(gatsby): wait for extracted query enqueuing before running it
1 parent 961f3d5 commit f50093e

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const author = `@gatsbyjs`
2+
3+
beforeEach(() => {
4+
cy.visit(`/page-query/`).waitForRouteChange()
5+
})
6+
7+
describe(`hot-reloading page queries`, () => {
8+
it(`displays placeholder content on launch`, () => {
9+
cy.getTestElement(`hot`).invoke(`text`).should(`not.contain`, author)
10+
})
11+
12+
it(`can edit a page query`, () => {
13+
cy.exec(
14+
`npm run update -- --file src/pages/page-query.js --replacements "# %AUTHOR%:author" --exact`
15+
)
16+
17+
cy.getTestElement(`hot`).invoke(`text`).should(`contain`, author)
18+
})
19+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from "react"
2+
import { graphql } from "gatsby"
3+
4+
function PageQuery({ data }) {
5+
return (
6+
<p data-testid="hot">
7+
{data.site.siteMetadata.title}: {data.site.siteMetadata.author}
8+
</p>
9+
)
10+
}
11+
12+
export const query = graphql`
13+
{
14+
site {
15+
siteMetadata {
16+
# %AUTHOR%
17+
title
18+
}
19+
}
20+
}
21+
`
22+
23+
export default PageQuery

packages/gatsby/src/state-machines/query-running/index.ts

+21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { queryActions } from "./actions"
77
* This is a child state machine, spawned to perform the query running
88
*/
99

10+
const PAGE_QUERY_ENQUEUING_TIMEOUT = 50
11+
1012
export const queryStates: MachineConfig<IQueryRunningContext, any, any> = {
1113
initial: `extractingQueries`,
1214
id: `queryRunningMachine`,
@@ -23,6 +25,25 @@ export const queryStates: MachineConfig<IQueryRunningContext, any, any> = {
2325
id: `extracting-queries`,
2426
src: `extractQueries`,
2527
onDone: {
28+
target: `waitingPendingQueries`,
29+
},
30+
},
31+
},
32+
// This state exists solely because "extractQueries" finishes too early.
33+
// It finishes before extracted queries are enqueued for execution.
34+
// As a result calculateDirtyQueries doesn't see them and they are not executed.
35+
//
36+
// This happens because extracted queries are enqueued for execution with setTimeout(x, 0)
37+
// wrapper in actions of redux/machines/page-component which fires after "extractQueries" finishes.
38+
//
39+
// see https://github.com/gatsbyjs/gatsby/issues/26580
40+
//
41+
// FIXME: this has to be fixed properly by not leaving "extractingQueries" state
42+
// until all extracted queries are enqueued for execution (but requires a refactor)
43+
waitingPendingQueries: {
44+
id: `waiting-pending-queries`,
45+
after: {
46+
[PAGE_QUERY_ENQUEUING_TIMEOUT]: {
2647
target: `writingRequires`,
2748
actions: `markSourceFilesClean`,
2849
},

0 commit comments

Comments
 (0)