7. FAQ: Troubleshooting Image Issues
Q. I added remotePatterns, but it still fails.
Did you restart the server? Changes in next.config.js are NOT hot-reloaded. You must stop (Ctrl+C) and start (npm run dev) again.
Q. Can I allow ALL domains?
Technically yes, but don't do it.
remotePatterns: [{ protocol: 'https', hostname: '**' }]
This opens your server to being an Open Proxy. Anyone can use your server to download any file from the internet, hiding their IP address.
Q. How to handle user-uploaded images?
If users upload images to S3, the keys (filenames) are unpredictable.
Allows the bucket domain, but restrict the path if possible:
{
protocol: 'https',
hostname: 'my-bucket.s3.amazonaws.com',
pathname: '/user-uploads/**',
}
Q. My images are not maintaining aspect ratio.
Use style={{ objectFit: 'cover' }} combined with fill prop if you don't know the exact dimensions.
If you know dimensions, always provide width and height props to prevent layout shift.
Q. Principles of Modern Image Formats (AVIF vs WebP)
Next.js automatically serves AVIF to browsers that support it (Chrome, Firefox) and falls back to WebP or JPEG for older ones.
AVIF offers superior compression (20% smaller than WebP) but takes longer to generate. If your build times are too slow, you can disable AVIF in next.config.js:
images: { formats: ['image/webp'] }
Q. Debugging "Forbidden" Errors
If remotePatterns looks correct but you still get errors, check if the image server (e.g., S3) blocks "hotlinking".
Next.js fetches the image from the server side. If your S3 bucket policy blocks requests that don't come from a browser (no Referer header), the optimization will fail. You may need to update your S3 Bucket Policy to allow access from your Next.js server's IP or User-Agent.