From bb25e5b84527dbd04aa3a0a5833b917fc7ec8f12 Mon Sep 17 00:00:00 2001 From: Tyler Barnes Date: Tue, 13 Apr 2021 09:03:46 -0700 Subject: [PATCH] fix(gatsby-source-wordpress): invalidate less queries during previews (#30770) --- .../update-nodes/wp-actions/update.js | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/gatsby-source-wordpress/src/steps/source-nodes/update-nodes/wp-actions/update.js b/packages/gatsby-source-wordpress/src/steps/source-nodes/update-nodes/wp-actions/update.js index e053c56f245ef..73a52d9ecd692 100644 --- a/packages/gatsby-source-wordpress/src/steps/source-nodes/update-nodes/wp-actions/update.js +++ b/packages/gatsby-source-wordpress/src/steps/source-nodes/update-nodes/wp-actions/update.js @@ -41,7 +41,7 @@ export const fetchAndCreateSingleNode = async ({ const query = getNodeQuery() const { - helpers: { reporter }, + helpers: { reporter, getNode }, pluginOptions, } = getGatsbyApi() @@ -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( @@ -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({