Part 66: Exploring Image Optimization in Next.js Production Builds
[App] Image Optimization

When building modern web applications, optimizing images is crucial for performance and user experience. Next.js provides a robust image optimization feature that helps developers manage images efficiently. In this post, we'll explore how images are handled in both development and production environments with Next.js, and delve into the options available for static exports and custom loaders.
Testing the Production Build
To understand how image optimization works in a production setting, we first need to create a production build of our application. This involves stopping the development server and running a build command to compile the app. Once the build is complete, we can start the server and observe how images are loaded.
Inspecting Network Requests
When we reload the page with an "empty cache and hard reload," we can examine the network requests to see how images are handled:
WebP Conversion: Images are automatically converted to the WebP format, which is more efficient than traditional formats like JPEG or PNG.
Priority Loading: The first image is loaded immediately after the stylesheet due to the
priorityproperty, while others utilize lazy loading.Optimized URLs: The image URLs point to a special Next.js path with parameters referencing the original image source. This indicates that Next.js is optimizing images on-the-fly.
Local Cache Inspection
In the production build, the optimized images are cached locally within the .next folder. This caching reduces load times and server requests for frequently accessed images.
Static Export and the "Unoptimized" Option
Next.js also allows for static exports, which are useful for deploying static sites. However, this requires a different approach to image handling.
Using the "Unoptimized" Option
When exporting a static site, we can set the unoptimized option to true in the Image component. This bypasses Next.js's server-side optimizations and uses the original image URLs directly:
Direct Loading: Images are loaded directly from the source (e.g., Strapi) without conversion or resizing.
Browser Optimizations: Despite not using server optimizations, the Image component still supports browser features like lazy loading and fetch priority.
Removing Unnecessary Images
During static export, images from the public folder are copied over. It's a good practice to remove any outdated images that are no longer needed to keep the build clean and lightweight.
Custom Loaders for External Hosting
For developers using external image hosting services, Next.js offers the flexibility of custom image loaders:
Loader Property: Set the
loaderproperty to specify a custom service like Cloudinary or Imgix.Custom Loader Functions: You can write your own loader function to generate image URLs based on parameters like width and quality.
Implementing a Custom Loader
A custom loader is defined as a function that returns a URL for the image. This function can be customized to integrate with any external service or CDN.
const customLoader = ({ src, width, quality }) => {
return `https://example.com/${src}?w=${width}&q=${quality || 75}`;
};Conclusion
Next.js's image optimization capabilities offer significant advantages in terms of performance and efficiency. While the default server-side optimizations are ideal for most use cases, the flexibility of static exports and custom loaders provides developers with the tools to tailor image handling to specific project needs. As we continue to leverage these features, we ensure that our web applications remain fast and responsive across all devices.
By understanding and utilizing these options, you can enhance your application's performance and deliver a seamless experience to your users. For more details on configuring the Image component and custom loaders, refer to the official Next.js documentation.
Last updated