.jpg)
Nov 9, 2024
•
#next.js
•
2 mins read
Hello everyone! In today’s tutorial, we’ll setup image optimization with Next.js static exports. I’ll walk you through a simple image gallery project to show how we can make images load faster, especially on mobile networks, using Next.js's next-image-export-optimizer.
I’ve created a simple image gallery project in VS Code with a local development server. Initially, it’s configured with the default Next.js settings. You can find a source code here.
But when I switch to static export mode (using output: "export" in next.config.js), an error appears: “Image optimization is not supported with export mode.”

There are two ways to handle this:
To use unoptimized images, set images.unoptimized to true in next.config.js. This allows images to load but at the cost of larger file sizes, which is inefficient, especially for mobile users.
next-image-export-optimizerTo optimize images without compromising static export capabilities, we can use the next-image-export-optimizer library. This tool converts images during the build process, generating multiple resolutions for different viewport sizes and saving bandwidth.
Here are the library’s main features:
package.json to automate the optimization on build:<Image> components with <OptimizedImage> provided by the library.After setting up, build the project by running:
npm run exportThe optimizer will generate multiple image resolutions in the output folder. When you serve the project locally, inspect the images in the network tab of your browser’s developer tools. You’ll see that different resolutions are loaded based on the device size, improving performance on slower networks.

quality setting in next.config.js (e.g., 80) to balance quality and performance.formats: ['webp'].blur: true. This loads low-res versions of images initially, enhancing user experience on slower networks.With next-image-export-optimizer, you can enjoy optimized images in your Next.js static exports without compromising performance. This setup helps deliver a responsive, efficient user experience on various devices. Thanks for reading!
https://www.npmjs.com/package/next-image-export-optimizer
https://nextjs.org/docs/pages/building-your-application/deploying/static-exports