dev3 ·
I get Invalid src prop ... hostname "cdn.example.com" is not configured under images in your next.config.js. How do I allow remote images and avoid layout shift?
0
DEV3 Developer Forum
Every post is reviewed first. Usually reviewed within 24 hours.
I get Invalid src prop ... hostname "cdn.example.com" is not configured under images in your next.config.js. How do I allow remote images and avoid layout shift?
next/image only optimises remote URLs that you explicitly allow, to prevent abuse of your optimiser.
// next.config.ts
import type { NextConfig } from "next";
const config: NextConfig = {
images: {
remotePatterns: [
{ protocol: "https", hostname: "cdn.example.com", pathname: "/uploads/**" },
],
},
};
export default config;
images.domains is deprecated in favour of remotePatterns. Restart the dev server after changing config.
Give dimensions, or fill a sized parent:
import Image from "next/image";
<Image src="https://cdn.example.com/uploads/a.jpg" alt="Team photo" width={800} height={450} />
<div className="relative aspect-video">
<Image src={url} alt="" fill sizes="(min-width: 768px) 50vw, 100vw" className="object-cover" />
</div>
priority only to the above-the-fold (LCP) image. Newer versions also accept preload; check your version.sizes with fill or responsive images, or the browser may download an oversized file.unoptimized.sharp for good performance (installed automatically by Next 15 in most setups).images.qualities / formats: ["image/avif","image/webp"] tune output.Pitfall: if the remote server blocks hotlinking or is slow, the optimiser returns 400/504. Fetch it yourself in a route or copy images to your own storage.
To shrink sources before upload, try the image compressor.