Skip to content

Commit ec2eded

Browse files
committed
add type guard to identify static images
1 parent 8bdcc01 commit ec2eded

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/components/Image.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@ const imageProps: (keyof NextImageProps)[] = [
3333
"useMap",
3434
]
3535

36+
const isStaticImageData = (src: ImageProps["src"]): src is StaticImageData => {
37+
return typeof src === "object" && "blurDataURL" in src
38+
}
39+
3640
const DefaultNextImage = (props: ImageProps) => {
37-
const hasBlurData = !!(
38-
(props.src as StaticImageData).blurDataURL || props.blurDataURL
39-
)
41+
if (isStaticImageData(props.src)) {
42+
return <NextImage placeholder="blur" {...props} />
43+
}
44+
45+
const hasBlurData = !!props.blurDataURL
4046
return <NextImage placeholder={hasBlurData ? "blur" : "empty"} {...props} />
4147
}
4248

0 commit comments

Comments
 (0)