Next.js Discord

Discord Forum

Sanity.io + Next.js

Answered
Little Gull posted this in #help-forum
Open in Discord
Little GullOP
Having a bit of an issue with images from blogposts made on the sanity.io dashboard not displaying images on my page
Ill supply the relevant code here :)
Page.tsx
export default async function Home() {
  const projects = await getProjects();

  return (
    <div className="max-w-5xl mx-auto py-20">
      <h1 className="text-7xl font-extrabold">Hello I&apos;m
        <span className="bg-gradient-to-r from-sky-500 via-violet-500 to-blue-700 bg-clip-text text-transparent"> Inside!</span>
      </h1>
      <p className="mt-3 text-xl text-gray-500">Hello! check out my projects here:</p>

      <h2 className="mt-24 font-bold text-gray-700 text-3xl">My Projects</h2>

      <div className="mt-5 grid md:grid-cols-2 lg:grid-cols-3 gap-8">
        {projects.map((project) => (
            <div key={project._id} className="border-2 border-gray-500 rounded-lg p-1 hover:scale-105 hover:border-blue-500 transition">
              {project.image && (
                <Image
                  src={project.image}
                  alt={project.name}
                  width={250}
                  height={100}
                  className="object-cover rounded-lg border border-gray-500"
                />
              )}
              <div className="mt-2 font-extrabold bg-gradient-to-r from-sky-500 via-violet-500 to-blue-700 bg-clip-text text-transparent">
                {project.name}
              </div>
            </div>
        ))}
      </div>
    </div>
  )
}

I'm not receiving any errors its just not showing the image as expected. There should be an image above the "test" text!
Answered by Little Gull
Closed: Image popped up on its own after re-running next.js several times. suspicions it may be due to this
Since you haven't set a value for `useCdn`, we will deliver content using our global, edge-cached API-CDN. If you wish to have content delivered faster, set `useCdn: false` to use the Live API. Note: You may incur higher costs using the live API.
View full answer

33 Replies

Little GullOP
Please let me know if there’s anything else I need to share :)
Little GullOP
Closed: Image popped up on its own after re-running next.js several times. suspicions it may be due to this
Since you haven't set a value for `useCdn`, we will deliver content using our global, edge-cached API-CDN. If you wish to have content delivered faster, set `useCdn: false` to use the Live API. Note: You may incur higher costs using the live API.
Answer
British Shorthair
hey @Little Gull
I used to work with sanity before and I think I know where the problem is
Little GullOP
Hello :)
Oh awesome!
British Shorthair
first check if your image url is correct by inspecting the <img />
it should be something like this: https://cdn.sanity.io/images/zp7mbokg/production/G3i4emG6B8JnTmGoN0UjgAp8-300x450.jpg
also on the sanity config for client
add the property useCdn: true
even though it doesn't cause the image problem
Little GullOP
export async function getProjects(): Promise<Project[]> {
const client = createClient({
projectId: "xxxxxx",

dataset: "blogposts",

apiVersion: "2023-07-26",

useCdn: "true",
});
Like that?
British Shorthair
ye
did you check your image element on the browser
@British Shorthair did you check your image element on the browser
Little GullOP
_next/image?url=https%3A%2F%2Fcdn.sanity.io%2Fimages%2F46r3hik6%2Fblogposts%2F7ade98b84632bfb02797b5adeac6a1133e458478-542x362.png&w=1920&q=75
That?
British Shorthair
visit the url in the browser
it's not showing for me
the url isn't correct
British Shorthair
it should be as I stated above
Little GullOP
Thats all im getting
British Shorthair
does it show up tho
Little GullOP
Yep :)
it fixed itself somehow earlier without me changing anything just took some time to upload
British Shorthair
why don't you use the native html image element
oh ok
@British Shorthair ye
Little GullOP
Type 'string' is not assignable to type 'boolean | undefined'.ts(2322)
index.d.ts(284, 3): The expected type comes from property 'useCdn' which is declared here on type 'ClientConfig'
British Shorthair
that's an easy fix
just change the "true" to true
from string to boolean, i didn't pay attention to your message that much in detail
Little GullOP
Yep im being dumb i havent had much sleep haha