Skip to content

Commit

Permalink
conditionally fetch image metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
smallbrownbike committed Sep 12, 2024
1 parent e6040f5 commit 4f00983
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
21 changes: 14 additions & 7 deletions gatsby/onCreateNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
: {}),
}
}
})
Expand Down
8 changes: 7 additions & 1 deletion plugins/gatsby-source-squeak/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,20 @@ 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: {
type: `SqueakRoadmap`,
contentDigest: createContentDigest(roadmap.attributes),
},
...rest,
media: cloudinaryMedia,
...(hasMedia ? { media: cloudinaryMedia } : {}),
...(image.data && {
id: createNodeId(`squeak-image-${image.data.id}`),
url: image.data.attributes.url,
Expand Down

0 comments on commit 4f00983

Please sign in to comment.