Skip to content

Commit

Permalink
fix(gatsby-source-wordpress): Add image cdn fields on updates (#35687)
Browse files Browse the repository at this point in the history
add image cdn fields on updates too
  • Loading branch information
TylerBarnes authored May 18, 2022
1 parent 7156882 commit 54832b4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 54832b4

Please sign in to comment.