I want to render my gatsby-image
conditionally: I want to have different images for mobile & desktop. So I need to swap them out.
Right now I am doing this:
<Desktop>
{heroImage && (
<MyGatsbyImage
img={heroImage}
/>
)}
</Desktop>
<Mobile>
{heroImageXS && (
<MyGatsbyImage
img={heroImageXS}
/>
)}
</Mobile>
where <Desktop>
& <Mobile>
are styled components with media-queries that have display: block / display:none
depending on the viewport.
However: Is this the most effective solution here? Is my solution always loading both images in the background?
Without gatsby-image
, I would do is this:
<picture>
<source
media="(min-width: 650px)"
srcset="images/img1.png">
<source
media="(min-width: 465px)"
srcset="images/img2.png">
<img src="images/img-default.png"
alt="a cute kitten">
</picture>
...but that would mean to not use gatsby-image
here - which I do want to use.