
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeI understand the issue you're experiencing. When you optimize images manually and repack them, the SHA1 hash in the asset IDs no longer matches the modified image files, which is why the images aren't showing up in your documents even though they appear in the media gallery.
Sanity image asset IDs follow a specific format: image-<sha1-hash>-<dimensions>-<format> (e.g., image-abc123def456-1920x1080-jpg). This SHA1 hash is a cryptographic fingerprint of the original image file contents. When you optimize the images:
assets.json and data.ndjson (or data.jsonld) files still reference the old hashThe images appear in the media gallery plugin because Sanity successfully imported the image files from the images folder and created new asset documents for them with new IDs based on the optimized files' SHA1 hashes. However, your content documents (in data.jsonld) still reference the old asset IDs from before optimization, so those references are broken.
You have a few options:
Instead of manually optimizing, let Sanity's Image Pipeline handle optimization on-demand. Sanity automatically serves optimized images via URL parameters without changing the source files:
// Images are automatically optimized at request time
imageUrlBuilder(client)
.image(asset)
.width(800)
.format('webp')
.quality(80)
.url()This way you keep original assets unchanged and get optimized delivery automatically through Sanity's CDN.
If you must optimize manually:
This would involve creating a migration that patches all documents with image fields:
// Pseudocode for migration
const assetMapping = {
'image-oldHash123-1920x1080-jpg': 'image-newHash456-1920x1080-jpg',
// ... map all your assets
}
// Update all documents with image references
documents.forEach(doc => {
if (doc.imageField?.asset?._ref) {
doc.imageField.asset._ref = assetMapping[doc.imageField.asset._ref]
}
})This is the most complex but "correct" approach if you want to maintain the export/import workflow:
<sha1>.<extension>assets.json with:_id (with new hash)sha1hash valuesize (file size in bytes)path to match new filenamedata.jsonld to use the new hash-based IDThis requires careful scripting to ensure consistency across all three files (images, assets.json, and data.jsonld).
The SHA1 hash system ensures content integrity and enables Sanity's deduplication—if the same image is uploaded twice, Sanity recognizes it by hash and stores only one copy. Changing file contents breaks this chain of trust.
For most use cases, Sanity's built-in image optimization via the CDN is the better approach—it's automatic, cached globally, supports WebP/AVIF formats, and doesn't require manual intervention or breaking asset references. The original high-quality assets remain unchanged in the Content Lake while optimized versions are served on-demand based on your URL parameters.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store