Guide
Using Gatsby StaticImage for Optimized Asset Delivery
Learn how to add a local image with Gatsby’s StaticImage component so the build creates multiple resolutions, serves WebP/Avif, and shows a blur placeholder.
Published by Tasadduq Burney
28 Jan 2026, 02:49 UTC
2 min62.5K views0

Desired outcome
Add a locally stored image to a Gatsby page so that the build pipeline creates multiple resized versions, serves WebP/Avif when supported, and shows a low‑quality blur placeholder while the image loads.
Prerequisites
- A Gatsby project (v4 or later) with Node.js ≥14 installed.
- Access to the project’s root directory to run npm commands.
- An image file you want to display, placed inside
src/images/(or any folder undersrc/).
Procedure
- Install the required plugins from the project root:
- Add the plugins to
gatsby-config.js. Ensuregatsby-plugin-sharpcomes beforegatsby-plugin-imagein the array: - Import and use the
StaticImagecomponent in a page or component. Replaceyour-image.jpgwith the actual file name: - Run the development server to see the result:
- For a production build, execute:
npm install gatsby-plugin-image gatsby-plugin-sharp
module.exports = {
plugins: [
'gatsby-plugin-sharp',
'gatsby-plugin-image',
// other plugins …
]
};
import { StaticImage } from 'gatsby-plugin-image'
export default function MyPage() {
return (
)
}
npm run develop
npm run build
npm run serve
Expected checks
- Open the page in a browser, right‑click the image and choose "Inspect". Verify that the
<img>element contains asrcsetattribute with multiple URLs (e.g., ...-400.webp, ...-800.webp). - Open the Network tab, reload the page, and filter by "Img". Confirm that the requested image URL ends with
.webpor.avif(depending on browser support). - Observe that a blurred version of the image appears briefly before the sharp image loads; this is the LQIP placeholder generated by the plugin.
Recovery options
If the image does not appear or the build fails:
- Check the file path in the
srcprop; an incorrect relative path triggers a build‑time error, not a 404. - Ensure
sharpis installed correctly; on some platforms you may need to runnpm rebuild sharpor install system dependencies (e.g.,libvipson Linux). - To revert, remove the
StaticImageusage, delete the plugin entries fromgatsby-config.js, rungatsby clean, and reinstall the previous state.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.