Next.js App Router Images Not Loading in Production
Issue
After migrating to Next.js 13+ with the new App Router, images that load correctly in development are not loading in the production build. The <Image />
component shows broken images or 404 errors, even though the paths are correct. What could be causing this issue and how can it be resolved?
Solution
This issue is often caused by misconfiguration of the next.config.js
file or incorrect usage of the Image
component with the new App Router. Here are some steps to resolve it:
-
Check the
images.domains
configuration: Ensure that all external domains used for images are listed in theimages.domains
array in yournext.config.js
. -
Use the correct
src
path: For static images in thepublic
folder, use/image.png
(with a leading slash). For images from remote sources, ensure the domain is allowed. -
Deployment platform settings: If deploying to Vercel or another platform, make sure the
public
directory is included and not ignored by.gitignore
or build settings. -
Clear build cache: Sometimes, a stale build cache can cause image issues. Run
next build && next start
after clearing.next
.
Example next.config.js
:
module.exports = {
images: {
domains: ['yourdomain.com', 'anotherdomain.com'],
},
};
By following these steps, you should be able to resolve most image loading issues in production with Next.js App Router.