Created
March 12, 2023 15:04
-
-
Save mikansc/332f48b2fcee11c0fe80c238d2cef95a to your computer and use it in GitHub Desktop.
Image component with performance configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import PropTypes from "prop-types"; | |
export const Image = (props) => { | |
const { src, alt } = props; | |
return ( | |
<picture> | |
<source | |
type="image/webp" | |
srcSet={` | |
${src}?width=100 100w, | |
${src}?width=200 200w, | |
${src}?width=400 400w, | |
${src}?width=800 800w | |
`} | |
/> | |
<img | |
src={src} | |
role="presentation" | |
alt={alt} | |
srcSet={` | |
${src}?width=100 100w, | |
${src}?width=200 200w, | |
${src}?width=400 400w, | |
${src}?width=800 800w | |
`} | |
loading="lazy" | |
sizes="(max-width: 800px) 100vw, 50vw" | |
decoding="async" | |
fetchPriority="high" | |
/> | |
</picture> | |
); | |
}; | |
Image.propTypes = { | |
src: PropTypes.string.isRequired, | |
alt: PropTypes.string.isRequired, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment