Skip to content

Commit c172848

Browse files
GatsbyJS Botvladar
GatsbyJS Bot
andauthored
fix(gatsby): fix stale query results on data updates (#28986) (#28994)
* Add failing test * fix(gatsby): fix stale query results on data updates * narrow the scope of the change * Address review comments * remove accidentally added stuff (cherry picked from commit 811c2a8) Co-authored-by: Vladimir Razuvaev <[email protected]>
1 parent f632c0a commit c172848

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

packages/gatsby/src/query/__tests__/data-tracking.js

+64
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,70 @@ describe(`query caching between builds`, () => {
666666
}, 999999)
667667
})
668668

669+
describe(`Changed node previously not used to be used by the query`, () => {
670+
let nodeChangeCounter = 1
671+
beforeEach(() => {
672+
setAPIhooks({
673+
sourceNodes: (nodeApiContext, _pluginOptions) => {
674+
const { createTestNode } = getTypedNodeCreators(nodeApiContext)
675+
676+
for (let i = 1; i <= nodeChangeCounter; i++) {
677+
createTestNode({
678+
id: `test-${i}`,
679+
slug: `foo${i}`,
680+
content: `Lorem ipsum.`,
681+
})
682+
}
683+
684+
nodeChangeCounter++
685+
},
686+
})
687+
setPageQueries({})
688+
setStaticQueries({
689+
"static-query-1": `
690+
{
691+
test(slug: { eq: "foo2" }) {
692+
slug
693+
content
694+
}
695+
}
696+
`,
697+
"static-query-2": `
698+
{
699+
test(slug: { eq: "foo3" }) {
700+
slug
701+
content
702+
}
703+
}
704+
`,
705+
})
706+
})
707+
708+
it(`rerunning after cache clearing - should run all queries`, async () => {
709+
const { staticQueriesThatRan } = await setup({
710+
restart: true,
711+
clearCache: true,
712+
})
713+
714+
// all queries to run
715+
expect(staticQueriesThatRan).toEqual([`static-query-1`, `static-query-2`])
716+
}, 99999)
717+
718+
it(`changing node to be used by any query triggers running that query (no restart)`, async () => {
719+
const { staticQueriesThatRan } = await setup()
720+
721+
// runs the query with filter `slug: { eq: "foo1" }`
722+
expect(staticQueriesThatRan).toEqual([`static-query-1`, `static-query-2`])
723+
}, 999999)
724+
725+
it(`changing node to be used by any query triggers running that query (with restart)`, async () => {
726+
const { staticQueriesThatRan } = await setup({ restart: true })
727+
728+
// runs the query with filter `slug: { eq: "foo2" }`
729+
expect(staticQueriesThatRan).toEqual([`static-query-2`])
730+
}, 999999)
731+
})
732+
669733
describe(`Changing data used in multiple queries properly invalidates them`, () => {
670734
let nodeChangeCounter = 1
671735
beforeAll(() => {

packages/gatsby/src/schema/node-model.js

+13
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,19 @@ class LocalNodeModel {
309309
this.trackInlineObjectsInRootNode(result)
310310
} else {
311311
result = null
312+
313+
// Couldn't find matching node.
314+
// This leads to a state where data tracking for this query gets empty.
315+
// It means we will NEVER re-run this query on any data updates
316+
// (even if a new node matching this query is added at some point).
317+
// To workaround this, we have to add a connection tracking to re-run
318+
// the query whenever any node of this type changes.
319+
if (pageDependencies.path) {
320+
this.createPageDependency({
321+
path: pageDependencies.path,
322+
connection: gqlType.name,
323+
})
324+
}
312325
}
313326
} else if (result) {
314327
result.forEach(node => this.trackInlineObjectsInRootNode(node))

0 commit comments

Comments
 (0)