Nextjs - How to Display Objects With Nextjs

14 replies
Last updated: Jun 6, 2022
Hi all, follow-up question to the one I asked earlier; I'm wondering what the best way to go about displaying my images with Nextjs is.
In my case, I'm rendering an object array with a name and an image, but I'm struggling to display the image properly (next Image fails to parse the src):


{logos[0].Partenaires.map((partner, i) => (
            <Image
              src={partner.partnerLogo.asset._ref}
              alt={partner.partnershipName}
              key={i}
              height={40}
              quality={100}
              layout="fill"
            />
          ))}

AI Update

The issue you're experiencing is that you're trying to pass partner.partnerLogo.asset._ref directly to the Next.js Image component, but the _ref is just a reference string (like "image-G3i4emG6B8JnTmGoN0UjgAp8-300x450-jpg"), not a usable image URL.

Here's how to properly display Sanity images with Next.js:

First, install the package:

npm install @sanity/image-url

Create a helper file (e.g., sanityImageUrl.ts):

import { createImageUrlBuilder } from '@sanity/image-url'
import type { SanityImageSource } from '@sanity/image-url'
import { client } from './client' // your Sanity client

const builder = createImageUrlBuilder(client)

export function urlFor(source: SanityImageSource) {
  return builder.image(source)
}

Then use it in your component:

import { urlFor } from './sanityImageUrl'
import Image from 'next/image'

{logos[0].Partenaires.map((partner, i) => (
  <Image
    src={urlFor(partner.partnerLogo).url()}
    alt={partner.partnershipName}
    key={i}
    width={200}  // specify actual dimensions
    height={40}
    quality={100}
  />
))}

Important notes:

  • Pass the entire partner.partnerLogo object (not just asset._ref) to urlFor() so it respects any crop/hotspot settings
  • Use width and height props instead of layout="fill" (deprecated in Next.js 13+)
  • Don't forget to configure remotePatterns in next.config.js:
module.exports = {
  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'cdn.sanity.io',
      },
    ],
  },
}

Solution 2: Query for the full URL in GROQ

Alternatively, you can get the URL directly in your GROQ query:

*[_type == 'yourType'] {
  Partenaires[]{
    partnershipName,
    "imageUrl": partnerLogo.asset->url
  }
}

Then use it directly:

<Image
  src={partner.imageUrl}
  alt={partner.partnershipName}
  width={200}
  height={40}
/>

The @sanity/image-url approach (Solution 1) is better because it automatically handles image transformations, respects editor-defined crops/hotspots, and lets you easily adjust image sizes on the fly with methods like .width(800) or .height(600).

Is
@sanity/image-url
the only way to get a url from these images?
You can also get the url by expanding the asset reference in your query using
->
.
so something like this? whereby
Parternaires
is my object array

*[_type=="informationsGenerales"]{
    Partenaires[] {
      ...,
      _type == "image => {
        "image": asset->url
      }
    },
    "image": image.asset->url
  }
What does your schema look like? If the object in the array is always going to have an image you may be able to skip the conditional!
this is the schema 🙂
I'm ok to skip the string field on line 22 if there's a way to attach an alt to the image coming out of Sanity
You can't attach an alt or anything like that in an out of the box image, but I think you can with the Media Plugin . It looks like we can drop that conditional and go with a named field. The following should get you the url:
*[_type=="informationsGenerales"]{
    Partenaires[] {
      ...,
      "imageUrl":image.asset->url
    },
  }
This is my output with that config:
this is when i log the output of
Partneraires
Oh I got it! I had to change
image
in the query to
partnerLogo
(what I named it in the schema).
AH, yeah, I was just looking at the top line of your schema and assumed that was the name. My own schema practices created selective blindness 🙂
no problem lol, i'm still new to writing these queries so I assumed image.asset was some sort of keyword 🤣
thanks for ur help!!
Glad we got it sorted out!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?