Issue with adding and using images in Sanity project, resolved by disabling cache in Next.js query.

42 replies
Last updated: Oct 2, 2023
Hey guys, I decided to try out sanity today after seeding some initial items, one image didn't successfully get added, so I added it manually using the studio and it looks like its there however when i map over within my next project and try and use that image i get an error 'Unable to resolve image URL from source', if i exclude that entry everything works fine. why is this?
AI Update

This is a common issue that happens when an image asset gets corrupted or doesn't upload completely. Based on a similar case from the Sanity community, there are a few likely causes and solutions.

Most Common Cause: Incomplete Asset Upload

When you manually add an image through the Studio, sometimes the asset doesn't complete uploading properly. This creates a malformed asset reference in your document. The image field might look fine in the Studio, but the underlying asset data is incomplete or missing critical information that @sanity/image-url needs to build the URL.

How to Fix It

1. Remove and re-upload the image

In the Studio, click the remove/delete option on that specific image field, publish the change, then re-upload the image fresh. This usually resolves the issue because it creates a complete, valid asset reference.

2. Check for Next.js caching issues (Most likely culprit!)

This is actually the most common reason if the image looks fine in the Studio but still fails in your Next.js project. Next.js (especially v13+) has aggressive caching that might be serving stale data. The error persists even after fixing the asset because your app is still using cached data from when the image was broken.

Try adding cache-busting options to your data fetching:

// For fetch calls
const data = await fetch(query, { cache: 'no-store' })

// Or use Next.js revalidation
const data = await fetch(query, { next: { revalidate: 0 } })

In the linked case, the developer resolved it by disabling Next.js's cache on that specific query. Clear your browser cache and restart your dev server after making this change.

3. Verify the asset structure

Console.log the problematic image object vs. a working one. A properly uploaded image asset should have a complete structure like:

{
  _type: 'image',
  asset: {
    _ref: 'image-abc123...',
    _type: 'reference'
  }
}

If the problematic image has null, undefined, or is missing the asset._ref field, that confirms a corrupted upload.

Why This Happens

The "Unable to resolve image URL from source" error from builder.image(source) specifically means the image URL builder can't find valid asset information to work with - it's receiving undefined or malformed data instead of a proper image asset reference. This typically happens when:

  • The initial seed/upload script didn't wait for the asset upload to complete
  • Network issues interrupted the upload
  • The asset was deleted but the reference remains
  • Next.js is caching old data that had a null value (most common!)

Prevention

When seeding data programmatically, make sure your image upload process completes fully before creating the document reference. If you're using the Sanity client to upload assets, wait for the upload promise to resolve completely before referencing the asset in your document.

Can you share your code?
sure which part would you like to see the react components or the sanity inventory? or something else?
i uploaded the project into git at https://github.com/aidycodes/sanity-first-look but you most likely dont want to look though the whole thing so let me know what your looking for and ill past it in a code block
also i noticed from console logging images that the the image i uploaded from image studio has completely different json
Yeah, it looks like the image didn’t complete uploading. Can you share a screenshot of what this field looks like in the Studio?
sure 1 sec
ok so i uploaded another image and checked the logs and it looked better so i removed my conditional which stopped that id being rendered but i stil get the same error
Can you try unsetting the field and re-adding the image? Something about the way you initially added it caused the asset to be malformed.
yes i just noticed that i have 2 images and not 1 on that id so the studio one isnt being used
when you say unset the field you mean delete the content?
Yes. Click the remove option on the item in the array.
ok so i removed everything from images and within my project they are still showing
so im gonna delete the cache from localhost
Did you also publish the changes?
yes
no lol
i thought i did
ok i deff have now and 2images are still showing so im just gonna remember how 2 delete the cache of a single website
ok so i cleared all site data and this is still showing up
i will test in another browser
Oh, just FYI you’ve pushed your token up with your repository. That’s something you should keep secret.
i only pushed it up quickly for you normally my next build wouldnt be so simple and its all protectedd
OK, cool cool.
Can you DM me your project ID and dataset name? I’m going to query your data to see what it looks like in the full context of the document.
NEXT_PUBLIC_SANITY_PROJECT_ID=kuq38qyv
how would i know my dataset name
is it products?
it would be the name you selected when you created the project via the CLI. The default is ‘production’. It’s probably in your .env as well.
lol yes it was next to it im dumb
NEXT_PUBLIC_SANITY_DATASET=production
Ok, the braided belt product is showing images as null on my end:
{
    "_createdAt": "2023-10-02T16:24:29Z",
    "name": "Braided Leather Belt",
    "sky": null,
    "description": "These handsome leather belts are guaranteed to pull together any outfit. They're made of vegetable-tanned Italian leather, which means they have natural highs and lows of color and will look even better over time.",
    "slug": null,
    "_id": "e882fe48-253c-40fb-8155-51b47b063c1a",
    "price": 4999,
    "currency": "USD",
    "images": null
  }
huh
So it looks like you have successfully removed the images. Can you try adding the images back in now?
okay ill do that i have a feeling it may not work tho
If the asset does not work, you’ll need to delete it and reupload\
ok so i put another image in
but im still getting the same error
if you query it however i think on your side it will look correct
i think next 13 is fetching stale data
ok so i fixed it by disabling next 13s cache on that query
thanks very much for taking the time to go though this with me ❤️
Oh great! I’m glad you found a solution 😮‍💨

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?