Skip to content

Instantly share code, notes, and snippets.

@whoisryosuke
Created April 26, 2018 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whoisryosuke/6e2bf8a1c511a970a5630609514c31e4 to your computer and use it in GitHub Desktop.
Save whoisryosuke/6e2bf8a1c511a970a5630609514c31e4 to your computer and use it in GitHub Desktop.
Best practice for importing static images into GatsbyJS to ensure lazyloading/scaling. If image is responsive (like background cover image), use sizes property instead of resolutions in query and <Img /> component. See: https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-image
import React, { Component } from "react";
import Img from "gatsby-image";
export default class Frontpage extends Component {
render() {
return (
<div className="Frontpage pt2">
<Img resolutions={this.props.data.PeaceEmoji.resolutions} alt="Peace sign emoji" />
</div>
);
}
};
export const query = graphql`
query IndexQuery {
PeaceEmoji: imageSharp (id: { regex: "/peace.png/" }) {
...emojiImageFields
},
}
fragment emojiImageFields on ImageSharp {
resolutions(width: 54, height: 54) {
...GatsbyImageSharpResolutions
}
}
`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment