Large and unoptimized images can dramatically slow down our site. Luckily, Gatsby has several useful plugins to optimize images on page components.
Using images in Gatsby requires the following plugins:
gatsby-image
can:
Before installing the gatsby-image plugin, ensure you have installed a source plugin, so your images are available in graphQl queries.
To install the gatsby-image plugin:
npm install gatsby-image
Depending on our starter theme, we may need to include gatsby-transformer-sharp and gatsby-plugin-sharp plugins.
gatsby-transformer-sharp and gatsby-plugin-sharp help to transform and process our images.
npm install gatsby-transformer-sharp gatsby-plugin-sharp
We then add the newly installed plugins to gatsby-config.js.
module.exports = {
plugins: [
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: path.join(__dirname, `src`, `images`),
},
},
`gatsby-plugin-sharp`,
`gatsby-transformer-sharp`,
],
};
We can use the gatsby-image in the following steps:
<Img />
component where we want to render the imagefluid
props (if the image is responsive) or fixed
props (if the image has a fixed size).Example We have a fixed field
import React from "react"
import { graphql } from "gatsby"
import Img from "gatsby-image"
export default ({ data }) => (
<div>
<h1>Hello gatsby-image</h1>
<Img fixed={data.file.childImageSharp.fixed} />
</div>
)
export const query = graphql`
query {
file(relativePath: { eq: "image-1.jpeg" }) {
childImageSharp {
fixed(width: 125, height: 125) {
...GatsbyImageSharpFixed
}
}
}
}
`
Gatsby provides query fragments that are reusable fields.
gatsby-image
currently include the fragments for gatsby-transformer-sharp
, gatsby-source-contentful
, gatsby-source-datocms
and gatsby-source-sanity
.
gatsby-transformer-sharp
gatsby-source-contentful
Example: If we have a fixed image, we have the following options:
We can display our image depending on the fragment.
Here is an example of fixed and fluid image query.
{
fixed: file(relativePath: {eq: "image-1.jpg"}) {
childImageSharp {
fixed(width: 400, grayscale: true) {
...GatsbyImageSharpFixed
}
}
}
fluid: file(relativePath: {eq: "image-2.jpg"}) {
childImageSharp {
fluid {
...GatsbyImageSharpFluid
}
}
}
}
import React from 'react'
import { useStaticQuery, graphql } from "gatsby"
import img from "../images/image-1.jpg"
import Img from "gatsby-image"
const getImages = graphql`
{
fixed: file(relativePath: {eq: "image-1.jpg"}) {
childImageSharp {
fixed(width: 400, grayscale: true) {
...GatsbyImageSharpFixed
}
}
}
fluid: file(relativePath: {eq: "image-2.jpg"}) {
childImageSharp {
fluid {
...GatsbyImageSharpFluid
}
}
}
}
`
const Images = () => {
const data = useStaticQuery(getImages)
return (
<section className="images">
<article className="single-image">
<h3>Basic image</h3>
<img src={img} width="30%" alt="beach" />
</article>
<article className="single-image">
<h3>Fixed image / Blur</h3>
<Img fixed={data.fixed.childImageSharp.fixed} />
</article>
<article className="single-image">
<h3>Fluid image</h3>
<Img fluid={data.fluid.childImageSharp.fluid} />
</article>
</section>
)
}
export default Images
The parent container will control the width of the fluid image.
maxWidth is one of the parameters of the fluid field, and it controls the size of images that will be generated using gatsby-image
For example, if we indicate the maxWidth to be 200px but our container is 800px, we will have a blurry image because it has to stretch to fix the container.
{
fluid: file(relativePath: {eq: "image-2.jpg"}) {
childImageSharp {
fluid (maxWidth: 200) {
...GatsbyImageSharpFluid
}
}
}
}