Next.js Discord

Discord Forum

Images not showing

Unanswered
Bonga shad posted this in #help-forum
Open in Discord
Bonga shadOP
Hi, I am unable to display images in my application.

I tried it both with Next.js Image component and with the Image component from @nextui-org/image.

My images are in the public folder and I am referencing it correctly as far as I can judge.
Not sure why I cant display them.

This is the error I get in my shell:
⨯ The requested resource isn't a valid image for /images/whatsapp-white.png received text/html; charset=utf-8

The image itself is showing. It's a .png image. I tried it with other images they don't work either.

14 Replies

Bonga shadOP
import { Navbar, NavbarBrand, NavbarContent, NavbarItem, Link, Button } from "@nextui-org/react";
// import { Image } from "@nextui-org/image";
import Image from "next/image";
import LangNav from "./LangNav";



export default function NavBar({ dict, lang }: { dict: any; lang: string }) {
  return (
    <Navbar className="bg-achi-blue  text-primary">
      <NavbarBrand>
        {/* <p className="font-bold text-inherit">ACHI</p> */}
        {/* <Image alt="whatsapp" width={50} height={50} src="/images/whatsapp-white.png" /> */}
      </NavbarBrand>
      <NavbarContent className="hidden sm:flex gap-[4em]" justify="center">
        <NavbarItem className="flex align-center">
            <span className="material-symbols-outlined mr-2">
            location_on
            </span>
            {dict.header.Lebanon}
        </NavbarItem>
        <NavbarItem className="flex align-center">
            <Link href="#" aria-current="page">
                <span className="material-symbols-outlined mr-2">
                    mail
                </span> 
                <span>
                    prego@achiponteggi.it
                </span>
            </Link>
        </NavbarItem>
        <NavbarItem>
          <Link href="#">
            <Image width={50} height={50} src="/images/whatsapp-white.png" alt="whatsapp"/>
          </Link>
        </NavbarItem>
      </NavbarContent>
      <NavbarContent justify="end">
        <NavbarItem>
            <LangNav dict={dict} lang={lang} />
        </NavbarItem>
      </NavbarContent>
    </Navbar>
  );
}
Sun bear
Maybe the problem is because of /app/[lang]

What do you see when you open /images/filename.png in browser? A 404 page?
Bonga shadOP
You mean http://localhost:3000/images/whatsapp-white.png ?
I do get a 404 page here
Did you add the middleware?
Bonga shadOP
My middleware.ts looks like this:

import { NextResponse, type NextRequest } from "next/server";
import { defaultLocale } from "@/constants/locales";
import { i18n } from "@/i18n-config";

export function middleware(request: NextRequest) {
  // Check if there is any supported locale in the pathname
  const { pathname } = request.nextUrl;

  if (
    pathname.startsWith(`/${defaultLocale}/`) ||
    pathname === `/${defaultLocale}`
  ) {
    // The incoming request is for /en/whatever, so we'll reDIRECT to /whatever
    return NextResponse.redirect(
      new URL(
        pathname.replace(
          `/${defaultLocale}`,
          pathname === `/${defaultLocale}` ? "/" : ""
        ),
        request.url
      )
    );
  }

  const pathnameIsMissingLocale = i18n.locales.every(
    (locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`
  );

  if (pathnameIsMissingLocale) {
    // Now for EITHER /en or /nl (for example) we're going to tell Next.js that the request is for /en/whatever
    // or /nl/whatever, and then reWRITE the request to that it is handled properly.
    return NextResponse.rewrite(
      new URL(
        `/${defaultLocale}${pathname}${request.nextUrl.search}`,
        request.nextUrl.href
      )
    );
  }
}

export const config = {
  matcher: [
    // Skip all internal paths (_next)
    "/((?!_next).*)",
    // Optional: only run on root (/) URL
    // '/'
  ],
};
It's working as intended but I am also new to Next.js
Bonga shadOP
@Sun bear still have not figured this out
@Bonga shad <@908094633660268555> still have not figured this out
Sun bear
I would recommend to follow along the docs. I think something in the middleware is causing this behaviot but difficult to see it by just looking at the code. Needs a bit trial and error
now the cause of your bug is that your middleware is trying to redircet from /your-image.png to /:locale/your-image.png. there are two ways to fix.

the first way is as above – then the image URL becomes /_next/static/... which is excluded by the middleware matcher. i recommend this way.

the second way is to modify the matcher so that static files are excluded too. it involves regex shenanigans and becomes pretty complicated.