Skip to content

Instantly share code, notes, and snippets.

@mikansc
Created March 12, 2023 15:04
Show Gist options
  • Save mikansc/332f48b2fcee11c0fe80c238d2cef95a to your computer and use it in GitHub Desktop.
Save mikansc/332f48b2fcee11c0fe80c238d2cef95a to your computer and use it in GitHub Desktop.
Image component with performance configuration
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