From 4f00983458fc12d2951c06fe657eca2e397efe77 Mon Sep 17 00:00:00 2001 From: Eli Kinsey Date: Thu, 12 Sep 2024 04:18:34 -0700 Subject: [PATCH] conditionally fetch image metadata --- gatsby/onCreateNode.ts | 21 ++++++++++++++------- plugins/gatsby-source-squeak/gatsby-node.ts | 8 +++++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/gatsby/onCreateNode.ts b/gatsby/onCreateNode.ts index ecf8e101feda..11544eb47a9f 100644 --- a/gatsby/onCreateNode.ts +++ b/gatsby/onCreateNode.ts @@ -115,15 +115,22 @@ export const onCreateNode: GatsbyNode['onCreateNode'] = async ({ if (!cloudinaryData) { console.warn(`Cloudinary data not found for ${publicId}`) } + + const hasData = publicId && cloudinaryData?.format && cloudinaryData?.width && cloudinaryData?.height + node.frontmatter[field] = { publicURL: node.frontmatter?.[field], - childImageSharp: { - cloudName: process.env.GATSBY_CLOUDINARY_CLOUD_NAME, - publicId, - originalFormat: cloudinaryData?.format, - originalWidth: cloudinaryData?.width, - originalHeight: cloudinaryData?.height, - }, + ...(hasData + ? { + childImageSharp: { + cloudName: process.env.GATSBY_CLOUDINARY_CLOUD_NAME, + publicId, + originalFormat: cloudinaryData?.format, + originalWidth: cloudinaryData?.width, + originalHeight: cloudinaryData?.height, + }, + } + : {}), } } }) diff --git a/plugins/gatsby-source-squeak/gatsby-node.ts b/plugins/gatsby-source-squeak/gatsby-node.ts index 8b7c0bb89e07..fee7e3a6113a 100644 --- a/plugins/gatsby-source-squeak/gatsby-node.ts +++ b/plugins/gatsby-source-squeak/gatsby-node.ts @@ -312,6 +312,12 @@ export const sourceNodes: GatsbyNode['sourceNodes'] = async ( originalFormat: (image?.data?.attributes?.ext || '').replace('.', ''), } + const hasMedia = + cloudinaryMedia.publicId && + cloudinaryMedia.originalHeight && + cloudinaryMedia.originalWidth && + cloudinaryMedia.originalFormat + const node = { squeakId: roadmap.id, internal: { @@ -319,7 +325,7 @@ export const sourceNodes: GatsbyNode['sourceNodes'] = async ( contentDigest: createContentDigest(roadmap.attributes), }, ...rest, - media: cloudinaryMedia, + ...(hasMedia ? { media: cloudinaryMedia } : {}), ...(image.data && { id: createNodeId(`squeak-image-${image.data.id}`), url: image.data.attributes.url,