How to optimize images at build (export) time using next/image
d-suke
Posted on June 13, 2022
As of May 2022, the only way to use next/image with next export is to use an external URL with an The only way to use next/image with next export is to use an external URL with an image provider.
(since next/image uses the Nodejs server-based image optimization API by default)
However, this is inconvenient if you are building a simple website or want to build one without a Node.js server.
Therefore, we have developed a library to solve this problem.
If you are looking for a solution to optimize your images at build time using next/image, this one is for you!
Using this repository, you can get the full benefits of next/image even when using next export by doing image optimization at build time.
This makes it possible to build a high performance website with this solution, whether you want to build a simple website or a completely static output.
Here are some examples of usage.
However, they are basically the same as the usage of next/image, so please refer to the official documentation here for details.
Set the placeholder
<Imageplaceholder="blur"src="/images/img.png"width={1920}height={1280}alt=""/>// Or import as follows<Imageplaceholder="blur"src={require('./img.png')}alt=""/>
Since this library is very similar to ours, it would be very good for you to try this one as well.
A brief comparison with our library reveals the following characteristics for your reference.
next-export-optimize-images
next-image-export-optimizer
All options for next/image are available
○
△ *1
Specify the image folder
None in particular (free)
Only in the specified directory
Optimize only the images you use
○
× *2
next/dymamic support
×
○
*1: Only strings can be specified in the src attribute.
*2: images of various widths with layout="intrinsic" (since 1x and 2x images are sufficient at this time).
Conclusion
Next.js is a very easy-to-use tool that can be used not only for web application development, but also for static sites.
Next.js is so easy to use that I think you will want to use it for static sites as well.
If you have any problems with image optimization, please use this library!
If you have any problems with this library or suggestions for features, please do not hesitate to contact me through
Issue or my Twitter!