Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export static images on Nextjs?

when I want to export my nextjs app, it says that I cannot export my images on static websites.

Error: Image Optimization using Next.js' default loader is not compatible with next export. Possible solutions: - Use next start to run a server, which includes the Image Optimization API. - Use any provider which supports Image Optimization (like Vercel). - Configure a third-party loader in next.config.js. - Use the loader prop for next/image.

How can I make it so that it does ?

Is there a way for me to simply tell it to render images statically ? I dont want to go throught other onlines images loaders..

like image 770
Cesarioo Avatar asked Jan 20 '26 16:01

Cesarioo


2 Answers

I created a npm module so that we can use the Next.js <Image/> component with optimized images while using the static export functionality.

https://www.npmjs.com/package/next-image-export-optimizer

The library wraps the <Image /> component of Next.js and automatically creates optimized images using sharp.js at export time.

It uses a custom loader to create a srcset for the <img /> that the <Image /> component of Next.js creates. Then at build/export time, the images inside the public/images folder (as an example) are optimized with sharp.js and copied into the build folder.

like image 193
Niels Avatar answered Jan 22 '26 05:01

Niels


You need to set up a custom image loader in Next.js

In your next.config.js file, add this property to the export:

images: {
  loader: "custom"
}

And make a script called loader.js that exports this:

function imageLoader({ src }) {
  return `/images/${src}`; // REPLACE WITH YOUR IMAGE DIRECTORY
}

module.exports = imageLoader;

For each Image component, set the loader prop manually:

const imageLoader = require("PATH TO loader.js");

<Image loader={imageLoader} />
like image 25
skara9 Avatar answered Jan 22 '26 05:01

skara9



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!