Skip to content

Commit

Permalink
fix(gatsby-source-wordpress): invalidate less queries during previews (
Browse files Browse the repository at this point in the history
…#30770) (#30942)

(cherry picked from commit bb25e5b)

Co-authored-by: Tyler Barnes <tyler@gatsbyjs.com>
  • Loading branch information
GatsbyJS Bot and TylerBarnes committed Apr 19, 2021
1 parent e98cb62 commit 9fdfc57
Showing 1 changed file with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const fetchAndCreateSingleNode = async ({
const query = getNodeQuery()

const {
helpers: { reporter },
helpers: { reporter, getNode },
pluginOptions,
} = getGatsbyApi()

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

const remoteNode = data[singleName]
let remoteNode = data[singleName]

if (!data || !remoteNode) {
reporter.warn(
Expand All @@ -89,6 +89,30 @@ export const fetchAndCreateSingleNode = async ({
id,
})

if (isPreview) {
const existingNode = getNode(id)

/**
* For Preview, revisions of a node type can have data that updates unecessarily
* This code block fixes that. The result being that less queries
* 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.
*/
if (existingNode) {
remoteNode = {
...remoteNode,
databaseId: existingNode.databaseId,
date: existingNode.date,
dateGmt: existingNode.dateGmt,
slug: existingNode.slug,
guid: existingNode.guid,
id: existingNode.id,
link: existingNode.link,
uri: existingNode.uri,
status: existingNode.status,
}
}
}

data[singleName] = remoteNode

const { additionalNodeIds, node } = await createSingleNode({
Expand Down

0 comments on commit 9fdfc57

Please sign in to comment.