next/image fails with The requested resource isn't a valid image
The requested resource isn't a valid image for /public/logo.png received text/html; charset=utf-8Short 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.
Fix it fast
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 52Match what you see, then jump to its fix
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 components2Fix
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-typeConfirmed by nextjs.org
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/null2Fix
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-typeConfirmed by nextjs.org
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
Web apps
AuthUserPoolException: Auth UserPool not configured in a Next.js app
Auth UserPool not configured
The error means an Amplify Auth call ran before Amplify.configure did.
3 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb apps
ESLint says an element in a list is missing its key prop
react/jsx-key
react/jsx-key fires when an element returned from map, Array.from or an array literal has no key.
3 causes, each with a fix