[NTP Next] Allow sanitized images to be lazy loaded (#31058)

This commit is contained in:
Kevin Smith
2025-09-08 11:32:50 -04:00
committed by GitHub
parent 1bee0c2a90
commit 3ea50554df
@@ -5,7 +5,7 @@
import * as React from 'react'
import { loadImage, placeholderImageSrc } from '../../lib/image_loader'
import { placeholderImageSrc } from '../../lib/image_loader'
interface Props {
src: string
@@ -13,14 +13,16 @@ interface Props {
}
export function SafeImage(props: Props) {
const [imageURL, setImageURL] = React.useState(placeholderImageSrc)
React.useEffect(() => {
const url = 'chrome://image?url=' + encodeURIComponent(props.src || '')
loadImage(url).then((loaded) => {
setImageURL(loaded ? url : placeholderImageSrc)
})
}, [props.src])
return <img src={imageURL} loading='lazy' className={props.className} />
return (
<img
src={
props.src
? 'chrome://image?url=' + encodeURIComponent(props.src)
: placeholderImageSrc
}
loading='lazy'
className={props.className}
onError={(event) => { event.currentTarget.src = placeholderImageSrc }}
/>
)
}