Optimize Gatsby images at build time with gatsby-plugin-image
Use gatsby-plugin-image to generate responsive WebP/AVIF images during your Gatsby build instead of shipping unoptimized files.
29 Jan 2026, 01:32 UTC

Images that load fast locally but slow in production
You place a hero photo in src/images, reference it in a React component, and run gatsby develop. The page feels quick on your laptop. After deployment, Lighthouse flags an enormous unoptimized JPEG that every mobile visitor downloads in full. Gatsby does not compress or resize images simply because they live in your source folder.
The useful takeaway: gatsby-plugin-image processes images during the static build using Sharp, a Node image processing library. Your production HTML receives responsive srcset markup—a list of image URLs with widths so the browser chooses the right size—plus modern formats and native lazy loading without extra client-side JavaScript.
What happens during the Gatsby build
Gatsby generates static HTML before deployment. gatsby-plugin-image hooks into that pipeline and runs Sharp over the images your components reference. It creates multiple widths, converts to WebP or AVIF where supported, and writes the results to the build directory. The component renders standard image tags with srcset and explicit width and height attributes, which helps prevent layout shift while the image loads.
StaticImage is for files with a fixed path known when you write the code. GatsbyImage is for images discovered through GraphQL, such as files from a content folder or CMS. Both require the source file to exist at build time.
Worked example: configure StaticImage
Run the install command in your Gatsby project root. Normal file-system permissions are sufficient unless your node_modules ownership is restricted.
npm install gatsby-plugin-image gatsby-source-filesystem sharp
Add the plugin and a filesystem source to gatsby-config.js. Replace src/images with your actual image directory.
module.exports = {
plugins: [
`gatsby-plugin-image`,
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: `${__dirname}/src/images`,
},
},
],
}
Import StaticImage in a page component and point it at a local file:
import { StaticImage } from 'gatsby-plugin-image'
export default function Hero() {
return (
)
}
layout='constrained' displays the image at its natural size up to the given width. objectFit='cover' tells the browser how to fill the allotted box without distorting the picture.
Run the production build:
npx gatsby build
Expected check: open the generated HTML in public and confirm the image markup includes srcset, width, height, and loading='lazy'. In development, run gatsby develop, open browser DevTools to the Network tab, reload the page, and verify the browser requests a WebP or AVIF variant and defers offscreen images.
Risk: gatsby build processes every referenced image, so a directory with hundreds of high-resolution photos can increase build time and memory usage.
Limitations to plan around
- Sharp native dependencies: Sharp compiles native code. On some ARM hosts or minimal Docker images without prebuilt binaries, installation can fail during the Sharp build step. Check the plugin README for supported Node versions before upgrading.
- Build-time only: If an image URL is unknown until runtime—such as a user upload or third-party API response—the plugin cannot optimize it. Use an external image service or client-side optimizer for those sources.
- Static references:
StaticImageneeds a statically analyzable path. Variable paths or dynamicrequirecalls may not be processed.
Verify before you deploy
After gatsby build, inspect the built HTML in public for srcset on image tags. Run gatsby develop and use DevTools Network to confirm offscreen images have loading='lazy' and the requested format matches what the plugin generated. If Sharp fails in your CI image, use a Docker base with the required build tools or pin a Sharp release with prebuilt binaries for your architecture.
Build-time image optimization removes the heaviest payload from your Gatsby pages without runtime JavaScript. Configure the plugin, point it at local images, inspect the build output, and ship smaller images by default.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.