SHANbic
SHANbic

Reputation: 211

NEXTJS - public images not showing on dynamic routes

I have a problem with serving a static image from my public/images folder.

It correctly displays in the files I've been working on so far EXCEPT the dynamic file [itemId].js (shown in the picture below)

The most interesting clue I have is this error in the console.

http://localhost:3000/items/_next/static/image/public/images/banner-big.f496e182c5dd297c1d1c030d9b7436d8.png?auto=format&fit=max&w=1920 404 (Not Found)

It tries to serve from /items, which is not what I want at all...

Does anyone know what's the trick behind this? If you need more informations, let me know.

// in components/Header.js (which is wrapping all of my pages)
import bannerBig from "../public/images/banner-big.png";

// then i use it this way with the Image component from next/image
 <Image src={bannerBig} alt="midnight city" width={1920} height={800}/>

The img generated by the <Image /> component looks like this and is the same accross all of my pages:

<img alt="midnight city" src="_next/static/image/public/images/banner-big.f496e182c5dd297c1d1c030d9b7436d8.png?auto=format&amp;fit=max&amp;w=3840" decoding="async" style="position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%" srcset="_next/static/image/public/images/banner-big.f496e182c5dd297c1d1c030d9b7436d8.png?auto=format&amp;fit=max&amp;w=1920 1x, _next/static/image/public/images/banner-big.f496e182c5dd297c1d1c030d9b7436d8.png?auto=format&amp;fit=max&amp;w=3840 2x">

project structure

Upvotes: 7

Views: 4274

Answers (1)

SHANbic
SHANbic

Reputation: 211

so I finally fixed the problem by adding a '/' before my static image path so it's always being served from the root

<Image 
    src={"/" + bannerBig.src}
    alt="midnight city"
    width={bannerBig.width}
    height={bannerBig.height}
/>

Upvotes: 14

Related Questions