Next.js image handling depends on the deployment mode and image source configuration. Runtime optimization needs a suitable server environment or a supported external image loader.
IN THIS ARTICLE
Check access and requirements
Identify whether the site uses a Node deployment or static export, and whether images are local or remote. Confirm memory and native dependency support for the selected optimizer.
01 Set up Next.js image optimization on hosting
Use the image configuration supported by the installed Next.js release. Restrict remote image patterns to the sources and paths the application actually needs.
For a self-hosted server, install and deploy the required compatible optimization dependencies. Test the target operating environment rather than copying native modules from another platform.
For static export, select a compatible custom loader or another deliberate image-delivery strategy. The default runtime optimizer is not created by uploading static files.
Choose image dimensions and responsive sizing that match the page layout. Avoid requesting needlessly large transformed images.
Review caching and resource use using representative image sizes and formats before enabling a high-volume catalogue.
02 Example: permit only your intended remote image directory
For a current Next.js version supporting these image options, merge this into the existing next.config.mjs. Replace the example host and path with a trusted image location you control:
export default {
images: {
remotePatterns: [{
protocol: 'https',
hostname: 'images.example.com',
port: '',
pathname: '/catalog/**',
search: '',
}],
maximumRedirects: 0,
},
};
This permits the selected directory, excludes query strings and prevents an allowed URL from redirecting the optimizer elsewhere. If your legitimate image service needs signed queries or redirects, design a narrowly reviewed policy rather than dropping the restrictions blindly.
Rebuild and deploy through the normal process. Verify an intended catalog image works and an unapproved host or path is rejected. This configures source acceptance; it does not provide the default runtime optimizer to a static export or replace sizing and dependency checks.
Confirm the expected result
Open a page at mobile and desktop widths and inspect actual image responses. Confirm allowed remote images load and an unapproved source is rejected.
Resolve setup problems
An optimizer error can come from a remote-source rule, inaccessible origin image, native dependency issue or resource limit. Check the specific response and server log before disabling all restrictions.
A common question
Are images optimized during every static export build?
Do not assume that. The selected loader and deployment mode determine where optimization happens. Configure an image service or prebuilt asset workflow that the exported site can actually use.