Skip to content

Next.js images return 400 after the remote URL changes

Diagnose a Next.js image optimizer 400 by comparing the source URL with remotePatterns, preserve a narrow allowlist, and verify the deployed image route.

By Crawlito · 6 min read

In short

  • The image optimizer can reject a remote URL that does not match the configured pattern.
  • Protocol, hostname, port, path, and query-string restrictions can each affect the match.
  • A working original image does not prove the application's optimized image route works.
On this page

If a Next.js image starts returning 400 after a CMS or CDN URL changes, compare the actual source URL with images.remotePatterns in the deployed configuration. A new hostname, folder, port, or query string can fall outside the allowed pattern. Correct the intended narrow match, deploy it, and verify the optimized image URL that the page actually requests.

Not every optimizer 400 has this cause. Read the response and server evidence first: other image parameters or source failures can produce a different rejection. A broad wildcard is not a substitute for identifying the mismatch.

Capture the optimizer request and original image

Open the failing page's Network panel and locate the image request. A default Next.js optimization route commonly carries the original source address as a query parameter.

Record that decoded source URL and the optimizer's response. Do not confuse the optimizer address with the original asset host.

Open the original public image separately and verify its status, content type, and actual contents. If it is already missing or protected, repair that boundary before changing the application's allowlist.

The Next.js Image reference documents remotePatterns and the restrictions applied to remote image sources. Use documentation matching the installed Next.js version, since image configuration evolves.

The Next.js readiness guide provides the wider page-delivery context. This guide focuses on the image request rather than treating every Next.js issue as a rendering-mode problem.

Keep private or signed query values out of shared screenshots and logs.

Break the source URL into the fields the pattern checks

Compare the observed source with the intended pattern field by field. A visually similar address can differ in a meaningful component.

For example, a migration may change assets.example.com to media.example.com, move files from one account folder to another, or append a version query.

Create a comparison table:

URL componentQuestion
ProtocolIs the source using the approved scheme?
HostnameIs this the intended asset service?
PortDoes the URL use an unexpected explicit port?
PathIs the file within the approved folder?
Search stringAre query parameters permitted as intended?

The Next.js documentation describes how these fields constrain the match. An empty search restriction and an omitted search restriction do not express the same policy.

Inspect the actual value produced by the CMS and component. A developer may test a clean URL while the public page uses one with a query string.

Write down the smallest difference that explains the rejection before changing configuration.

Confirm which configuration reached production

Read the configuration used by the deployed release, not only the current local file. A correct local pattern cannot affect a production build that has not incorporated it.

Check whether the application uses the default optimizer, a custom loader, or another image service. A custom loading path can change which configuration is relevant.

If the original source URL is assembled from environment values, verify the generated production address. A staging asset host can leak into a production component even while the visible page uses the correct domain.

Keep the request timestamp and deployment identity together. This helps distinguish an old build from a current configuration error.

Do not edit the image component's alt text or structured data as a response to a transport rejection. Those fields do not authorize the optimizer to fetch an unmatched source.

If several applications share the CDN, inspect the configuration for the specific app serving the failing optimizer route. A pattern in another repository is not evidence that this deployment permits the image.

Define a narrow pattern for the intended public assets

An illustrative configuration for one account folder can look like this:

javascript
const nextConfig = {
  images: {
    remotePatterns: [{
      protocol: "https",
      hostname: "media.example.com",
      port: "",
      pathname: "/catalog-public/**",
      search: ""
    }]
  }
};

export default nextConfig;

This example deliberately excludes query strings. Adapt it to the real asset contract rather than pasting it into an unrelated deployment.

If the CMS adds a meaningful supported query, decide how to permit that request according to the current Next.js options. Do not remove every restriction simply because one versioned URL failed.

Preserve tenant or account boundaries on shared media hosts. Allowing an entire multi-tenant host can admit sources unrelated to your site.

For a source that requires private credentials, review the supported architecture instead of trying to make it behave like a public remote image. Keep authorization and server-side fetch behavior deliberate.

The image CDN guide helps separate public asset delivery from the page that embeds it.

Deploy and verify the actual optimized response

Run the application's required checks and deploy the configuration through its normal workflow. Then repeat the original image request from the public page.

Confirm that the optimizer returns an actual image with the expected content. A different placeholder or error image is not sufficient just because it has a successful status.

Inspect the resulting element's selected image URL, including responsive candidates where applicable. The browser can choose a different request from the one first inspected in the component source.

Test a second intended image from the same folder and one deliberately out-of-scope source in an approved test environment. The change should permit the intended source without silently broadening the entire fetch policy.

Do not rely only on a direct request to the original CDN asset. The failing boundary was the application's optimization route.

Keep the deployed configuration and the successful public response in the verification record.

Check other causes when the pattern already matches

Read the optimizer's actual error text and server logs if the source matches the deployed pattern. An invalid width, unsupported quality request, unavailable source, or unsupported input can require another repair.

At the time of writing, Next.js documents allowed image-quality values and behavior that can differ between component-generated requests and direct optimizer calls. Check the installed version rather than assuming every 400 is a remotePatterns failure.

Inspect redirects and the source's returned content. A valid-looking asset address may ultimately lead to an authentication page or an expired temporary URL.

The temporary S3 link guide explains why a time-limited source can work at launch and fail later.

Do not broaden a pattern to compensate for a source that no longer exists. Restore the intended asset or update the publishing reference.

Keep each confirmed cause distinct in the report so a future recurrence is not automatically assigned to the last configuration field you changed.

Verify the page's visual and discovery context

Open the article or product page at representative viewport sizes and confirm that the intended image displays without disrupting the layout.

Check the image's meaningful alternative text and nearby explanation. A repaired download does not automatically make the page understandable when images are unavailable.

Inspect public metadata if the image also serves as a social or structured-data asset. Those consumers may request a different address from the optimized browser image.

For a migration, sample older content as well as newly uploaded images. Different folders or hosts can coexist in the CMS long after the upload configuration changes.

Report the concrete outcome: the deployed optimizer accepts the intended source and the public page displays the right asset. Do not turn that into a promise of image indexing or AI citation.

Keep the narrow pattern's purpose documented so later CDN changes trigger an intentional update rather than a wildcard workaround.

Frequently asked questions

Why does the original image work while Next.js returns 400?

The optimizer applies its own request validation and delivery rules. Inspect the source URL, deployed configuration, and actual rejection.

Should I allow every remote hostname?

No. Permit the intended public sources and paths. A broad allowance can admit unrelated fetches and hide the actual publishing mismatch.

Does changing next.config locally repair the live site?

Only after the relevant configuration reaches the deployed application. Verify the public optimizer route after release.

Use the free Crawlito check on the repaired page to inspect its public content and metadata alongside the image-delivery test.

Filed under Platform guides. Topics: nextjs, images, troubleshooting