From 54832b4fb4a61614b8a96487b1bf894799c52bcf Mon Sep 17 00:00:00 2001 From: Tyler Barnes Date: Wed, 18 May 2022 12:15:33 -0700 Subject: [PATCH] fix(gatsby-source-wordpress): Add image cdn fields on updates (#35687) add image cdn fields on updates too --- .../fetch-referenced-media-items.js | 62 +++++++++++-------- .../update-nodes/wp-actions/update.js | 25 +++++--- 2 files changed, 51 insertions(+), 36 deletions(-) diff --git a/packages/gatsby-source-wordpress/src/steps/source-nodes/fetch-nodes/fetch-referenced-media-items.js b/packages/gatsby-source-wordpress/src/steps/source-nodes/fetch-nodes/fetch-referenced-media-items.js index 32a5264864113..d6a9d1842ef62 100644 --- a/packages/gatsby-source-wordpress/src/steps/source-nodes/fetch-nodes/fetch-referenced-media-items.js +++ b/packages/gatsby-source-wordpress/src/steps/source-nodes/fetch-nodes/fetch-referenced-media-items.js @@ -107,6 +107,33 @@ const pushPromiseOntoRetryQueue = ({ }) } +export const addImageCDNFieldsToNode = (node, pluginOptions) => { + if (!node?.__typename?.includes(`MediaItem`)) { + return node + } + + const placeholderUrl = getPlaceholderUrlFromMediaItemNode(node, pluginOptions) + + const url = node.sourceUrl || node.mediaItemUrl + + const filename = + node?.mediaDetails?.file?.split(`/`)?.pop() || + path.basename(urlUtil.parse(url).pathname) + + return { + ...node, + url, + contentType: node.contentType, + mimeType: node.mimeType, + filename, + filesize: node?.mediaDetails?.fileSize, + width: node?.mediaDetails?.width, + height: node?.mediaDetails?.height, + placeholderUrl: + placeholderUrl ?? node?.mediaDetails?.sizes?.[0]?.sourceUrl ?? url, + } +} + export const createMediaItemNode = async ({ node, helpers, @@ -180,35 +207,18 @@ export const createMediaItemNode = async ({ ) } - const placeholderUrl = getPlaceholderUrlFromMediaItemNode( - node, + node = addImageCDNFieldsToNode( + { + ...node, + parent: null, + internal: { + contentDigest: createContentDigest(node), + type: buildTypeName(`MediaItem`), + }, + }, pluginOptions ) - const url = node.sourceUrl || node.mediaItemUrl - - const filename = - node?.mediaDetails?.file?.split(`/`)?.pop() || - path.basename(urlUtil.parse(url).pathname) - - node = { - ...node, - url, - contentType: node.contentType, - mimeType: node.mimeType, - filename, - filesize: node?.mediaDetails?.fileSize, - width: node?.mediaDetails?.width, - height: node?.mediaDetails?.height, - placeholderUrl: - placeholderUrl ?? node?.mediaDetails?.sizes?.[0]?.sourceUrl ?? url, - parent: null, - internal: { - contentDigest: createContentDigest(node), - type: buildTypeName(`MediaItem`), - }, - } - if (localFileNode?.id) { node.localFile = { id: localFileNode?.id, 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 a52c0fc605d27..693859e4cb148 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 @@ -5,7 +5,9 @@ import chalk from "chalk" import { getQueryInfoBySingleFieldName } from "../../helpers" import { getGatsbyApi } from "~/utils/get-gatsby-api" import { CREATED_NODE_IDS } from "~/constants" -import fetchReferencedMediaItemsAndCreateNodes from "../../fetch-nodes/fetch-referenced-media-items" +import fetchReferencedMediaItemsAndCreateNodes, { + addImageCDNFieldsToNode, +} from "../../fetch-nodes/fetch-referenced-media-items" import { dump } from "dumper.js" import { atob } from "atob" @@ -194,16 +196,19 @@ export const createSingleNode = async ({ const builtTypename = buildTypeName(typeInfo.nodesTypeName) - let remoteNode = { - ...processedNode, - __typename: builtTypename, - id: id, - parent: null, - internal: { - contentDigest: createContentDigest(updatedNodeContent), - type: builtTypename, + let remoteNode = addImageCDNFieldsToNode( + { + ...processedNode, + __typename: builtTypename, + id: id, + parent: null, + internal: { + contentDigest: createContentDigest(updatedNodeContent), + type: builtTypename, + }, }, - } + pluginOptions + ) const typeSettings = getTypeSettingsByType({ name: typeInfo.nodesTypeName,