Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

next/image fails with The requested resource isn't a valid image

ErrorThe requested resource isn't a valid image for /public/logo.png received text/html; charset=utf-8

Short answer

The image optimizer asked for the file and got an HTML page back, usually a 404 page or a redirect to a login. Either the src path is wrong because public is part of it, or a Proxy rule answers the request before the image is served. Fix the src, then exclude image paths from the matcher.

  • Applies to Next.js 15, Next.js 16
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the exact src path in the browser. If you see a page and not the picture, the optimizer is receiving that same page.

    curl -sI http://localhost:3000/logo.png | head -n 5
  2. 2Match what you see, then jump to its fix

    1. 1The error path itself begins with /public/The src starts with /publicFix
    2. 2The path is correct and the file exists, but the response is a redirect or a login pageProxy (middleware) answers the image requestFix

What does the error look like?

  • The requested resource isn't a valid image for /public/logo.png received text/html; charset=utf-8

Cause 1 of 2

The src starts with /public

1Check

The Next.js docs say files in public are referenced from the base URL, so public/avatars/me.png is served at /avatars/me.png. The folder name is not part of the URL.

grep -rn "src=\"/public\|src='/public" app components

2Fix

Remove public from the path.

// wrong
<Image src="/public/logo.png" width={120} height={40} alt="Logo" />

// right
<Image src="/logo.png" width={120} height={40} alt="Logo" />

3Verify

Request the corrected URL; it should answer with an image type.

curl -sI http://localhost:3000/logo.png | grep -i content-type

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 2

Proxy (middleware) answers the image request

1Check

The Proxy docs say that without a matcher it runs on every request, including _next/image and public assets, so auth logic or redirects can block images.

grep -n "matcher" proxy.ts middleware.ts 2>/dev/null

2Fix

Use a matcher that leaves out static files and image optimisation paths.

export const config = {
  matcher: [
    '/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)',
  ],
}

3Verify

Reload while signed out; the image should still load.

curl -sI http://localhost:3000/logo.png | grep -i content-type

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Reference public files by their URL path, and test one by opening it directly.
  • Keep a negative-lookahead matcher in every Proxy file.
  • Prefer a static import of the image when it lives in your repo.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.