Skip to content

Commit

Permalink
add check for node.gatsbyImage in the getImage helper
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerBarnes committed Apr 27, 2022
1 parent 1917291 commit f8956bd
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/gatsby-plugin-image/src/components/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ export function hasImageLoaded(cacheKey: string): boolean {
export type IGatsbyImageDataParent<T = never> = T & {
gatsbyImageData: IGatsbyImageData
}
export type IGatsbyImageParent<T = never> = T & {
gatsbyImage: IGatsbyImageData
}
export type FileNode = Node & {
childImageSharp?: IGatsbyImageDataParent<Node>
}
Expand All @@ -61,14 +64,29 @@ const isGatsbyImageDataParent = <T>(
node: IGatsbyImageDataParent<T> | any
): node is IGatsbyImageDataParent<T> => Boolean(node?.gatsbyImageData)

export type ImageDataLike = FileNode | IGatsbyImageDataParent | IGatsbyImageData
const isGatsbyImageParent = <T>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
node: IGatsbyImageParent<T> | any
): node is IGatsbyImageParent<T> => 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
}

Expand Down

0 comments on commit f8956bd

Please sign in to comment.