Skip to content

Commit bb25e5b

Browse files
authored
fix(gatsby-source-wordpress): invalidate less queries during previews (#30770)
1 parent 25891cc commit bb25e5b

File tree

1 file changed

+26
-2
lines changed
  • packages/gatsby-source-wordpress/src/steps/source-nodes/update-nodes/wp-actions

1 file changed

+26
-2
lines changed

packages/gatsby-source-wordpress/src/steps/source-nodes/update-nodes/wp-actions/update.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const fetchAndCreateSingleNode = async ({
4141
const query = getNodeQuery()
4242

4343
const {
44-
helpers: { reporter },
44+
helpers: { reporter, getNode },
4545
pluginOptions,
4646
} = getGatsbyApi()
4747

@@ -71,7 +71,7 @@ export const fetchAndCreateSingleNode = async ({
7171
errorContext: `Error occurred while updating a single "${singleName}" node.`,
7272
})
7373

74-
const remoteNode = data[singleName]
74+
let remoteNode = data[singleName]
7575

7676
if (!data || !remoteNode) {
7777
reporter.warn(
@@ -89,6 +89,30 @@ export const fetchAndCreateSingleNode = async ({
8989
id,
9090
})
9191

92+
if (isPreview) {
93+
const existingNode = getNode(id)
94+
95+
/**
96+
* For Preview, revisions of a node type can have data that updates unecessarily
97+
* This code block fixes that. The result being that less queries
98+
* are invalidated in Gatsby. For example if you have a query where you're getting the latest published post using the date field, that should be static but each preview updates the date field on the node being previewed (because the revision has a new date). So if we prevent the following fields from changing, this will be less problematic.
99+
*/
100+
if (existingNode) {
101+
remoteNode = {
102+
...remoteNode,
103+
databaseId: existingNode.databaseId,
104+
date: existingNode.date,
105+
dateGmt: existingNode.dateGmt,
106+
slug: existingNode.slug,
107+
guid: existingNode.guid,
108+
id: existingNode.id,
109+
link: existingNode.link,
110+
uri: existingNode.uri,
111+
status: existingNode.status,
112+
}
113+
}
114+
}
115+
92116
data[singleName] = remoteNode
93117

94118
const { additionalNodeIds, node } = await createSingleNode({

0 commit comments

Comments
 (0)