Next.jsEasy
How do you configure remote image domains in Next.js?
Add allowed sources to images.remotePatterns in next.config.ts, specifying protocol, hostname, and optionally port, pathname and search. Since Next.js 15.3 you can also pass new URL("https://cdn.example.com/**"). The older images.domains option is deprecated in Next.js 16.
Why it is required
The image optimizer fetches the remote URL on your server. Without an allowlist, anyone could use your server to process arbitrary images (cost and SSRF risk). Unlisted URLs return 400.
Pattern details
protocol:"https"(avoid allowinghttp).hostname: exact (cdn.acme.com) or wildcard:*.acme.commatches one subdomain level,**.acme.commatches any depth.pathname:/uploads/**limits the path;**matches any depth,*one segment.port: usually empty.search: exact query string, if required.
Be as specific as possible: allowing ** on a shared host like s3.amazonaws.com lets anyone's bucket through.
Related Next.js 16 settings
images.localPatternsto allow local images with query strings.images.dangerouslyAllowLocalIPonly for private networks where images resolve to internal IPs.images.maximumRedirects(default 3).
Alternatives
unoptimizedon a single image skips the optimizer, so no pattern is needed.- A custom
loadersends the image straight to an external CDN that does the resizing.
Code example
tsx
1// next.config.ts2import type { NextConfig } from "next";34const nextConfig: NextConfig = {5 images: {6 remotePatterns: [7 // Shorthand (15.3+)8 new URL("https://images.unsplash.com/**"),9 // Object form with tight limits10 {11 protocol: "https",12 hostname: "**.acme-cdn.com",13 pathname: "/uploads/**",14 },15 {16 protocol: "https",17 hostname: "avatars.githubusercontent.com",18 pathname: "/u/**",19 },20 ],21 // domains: ["images.unsplash.com"], // deprecated in Next.js 1622 },23};2425export default nextConfig;
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.