From f8956bde8a37606b0972daf20c1d3e0221eaf119 Mon Sep 17 00:00:00 2001 From: Tyler Barnes Date: Wed, 27 Apr 2022 14:15:44 -0700 Subject: [PATCH] add check for node.gatsbyImage in the getImage helper --- .../src/components/hooks.ts | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-image/src/components/hooks.ts b/packages/gatsby-plugin-image/src/components/hooks.ts index 3e766d85a8c84..25ec01ebeece7 100644 --- a/packages/gatsby-plugin-image/src/components/hooks.ts +++ b/packages/gatsby-plugin-image/src/components/hooks.ts @@ -45,6 +45,9 @@ export function hasImageLoaded(cacheKey: string): boolean { export type IGatsbyImageDataParent = T & { gatsbyImageData: IGatsbyImageData } +export type IGatsbyImageParent = T & { + gatsbyImage: IGatsbyImageData +} export type FileNode = Node & { childImageSharp?: IGatsbyImageDataParent } @@ -61,14 +64,29 @@ const isGatsbyImageDataParent = ( node: IGatsbyImageDataParent | any ): node is IGatsbyImageDataParent => Boolean(node?.gatsbyImageData) -export type ImageDataLike = FileNode | IGatsbyImageDataParent | IGatsbyImageData +const isGatsbyImageParent = ( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + node: IGatsbyImageParent | any +): node is IGatsbyImageParent => Boolean(node?.gatsbyImage) + +export type ImageDataLike = + | FileNode + | IGatsbyImageDataParent + | IGatsbyImageParent + | IGatsbyImageData + export const getImage = (node: ImageDataLike): IGatsbyImageData | undefined => { if (isGatsbyImageData(node)) { return node } + // gatsbyImageData GraphQL field if (isGatsbyImageDataParent(node)) { return node.gatsbyImageData } + // gatsbyImage GraphQL field for Gatsby's Image CDN service + if (isGatsbyImageParent(node)) { + return node.gatsbyImage + } return node?.childImageSharp?.gatsbyImageData }