Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative path image import from JSON doesn't load in Next.js [duplicate]

JSON file data fraction:

{
  "categories": [
    {
      "id": 1,
      "category_slug": "food_supplements",
      "title": "Food Supplements",
      "image": "/../../public/images/foodSupplements.png",
    }
  ]
}

Component data fraction that renders the image:

{
  Data.categories.map((category, idx) => {
    return (
      <div key={idx} className="header-categories-container">
        <Image className="header-btn-image" src={category.image} alt="btn-img" width="64" height="64"></Image>
        <Link href={`/${category.route}`}>
          <button className="header-category-button">{category.title}</button>
        </Link>
      </div>
    )
  })
}

The error that occurs in the console is the following: The requested resource isn't a valid image for /../../public/images/foodSupplements.png received text/html; charset=utf-8

Tried putting images into different sources, still didn't work. Tried to import with src=require(...), still the same error.

like image 633
rlevec Avatar asked Dec 01 '25 11:12

rlevec


2 Answers

Try

{
  "categories": [
    {
      "id": 1,
      "category_slug": "food_supplements",
      "title": "Food Supplements",
      "image": "/images/foodSupplements.png",
    }
  ]
}

Analysis
If you have images folder in the public folder then during runtime (in browser) you can fetch the assets from the public folder directly i.e. /images/foodSupplements.png = public/images/foodSupplements.png

like image 110
codinn.dev Avatar answered Dec 02 '25 23:12

codinn.dev


Add that file to public folder and change image path to this.

"image": "/foodSupplements.png", should fix the issue

like image 32
Shahed Avatar answered Dec 02 '25 23:12

Shahed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!