Invalid hostname provided to Image component in next/image configuration
0 reputation · 15 Nov 2021, 22:22 UTC
0 reputation · 15 Nov 2021, 22:22 UTC
When using the <Image> component to fetch assets from external sources, providing a remote URL not explicitly defined in the next.config.js file results in a render-time failure. This prevents the internal image optimizer from processing or serving the asset.
There is significant architectural ambiguity between utilizing the legacy images.domains array and the modern images.remotePatterns configuration. While remotePatterns allows for granular control over protocols, ports, and path patterns, the domains approach remains prevalent in existing codebases and tutorials.
Furthermore, the wildcard matching behavior within remotePatterns—specifically how multi-segment versus single-segment wildcards are handled—can lead to unexpected hostname rejections across different Next.js versions.
domains to remotePatterns in Next.js 14+remotePatterns schema?The render-time error Invalid src prop ... hostname is not configured means the URL's host is not in your next.config.js allowlist. The optimizer refuses to fetch it. For a single failing host, add one scoped entry:
// next.config.js
module.exports = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'cdn.example.com',
pathname: '/images/**',
},
],
},
};Then restart the dev server — next.config.js changes are not hot-reloaded, which is the most common reason a "correct" config appears not to work. Verify by requesting /_next/image?url=... directly and confirming a 200 response.
images.domains is a legacy string array. Each entry implicitly allows HTTP and HTTPS, any port, and any path on that host. That is broader than most teams intend: the Next.js image optimizer fetches arbitrary URLs server-side, so an over-broad allowlist effectively turns your server into a partial open proxy for remote content.
remotePatterns tightens this in three ways:
protocol: 'https' so plain-HTTP sources are rejected.pathname: '/images/**' prevents the optimizer from proxying unrelated content on the same host.Avoid hostname: '**'; it negates the entire security benefit. Both keys can coexist, but mixing them is confusing — pick remotePatterns and convert every domains entry.
This is where most "still failing after config change" reports come from:
| Pattern | Matches | Does not match |
|---|---|---|
*.example.com | a.example.com (exactly one level) | a.b.example.com, example.com |
**.example.com | Any depth of nested subdomains | example.com (apex) |
The apex domain is never covered by a subdomain wildcard — list it as a separate entry with hostname: 'example.com' if you need it. Test the boundaries directly: request images from a.example.com, a.b.example.com, and example.com to confirm which pattern your asset URLs actually require.
Wildcard handling and deprecation warnings for domains have shifted across Next.js releases. The behavior above reflects the documented Next.js 14-era semantics; run npx next info and check your installed version against the official remotePatterns documentation for that major version before relying on edge-case matching. As a final sanity check, request an image from a host you did not whitelist and confirm it is still rejected — this verifies your migration did not accidentally widen the allowlist.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.