Sanity Asset Optimisation
Serve Sanity images efficiently with structured GROQ image payloads, responsive sizing, and URL transforms that reduce payload and preserve visual quality.
Overview
The starter’s media path optimises performance by combining:
- schema-level image metadata (alt, hotspot, fit mode, caption)
- query-level image partials (including low-quality preview data)
- component-level responsive rendering with explicit
sizes - URL builder transforms for automatic format negotiation
Primary touchpoints in the enterprise starter:
packages/sanity-toolkit/studio/schema/fields/defineImageField.tsxapps/web/src/lib/sanity/query-partials/ImagePartial.tsapps/web/src/components/primitives/images/SanityImage.tsxapps/web/src/components/primitives/images/BaseImage.tsxapps/web/src/lib/sanity/data/urlFor.tsapps/web/src/features/seo/utilities/images.ts
Asset strategy
1) Capture useful image metadata in Studio
defineImageField(...) enables:
- hotspot-based art direction
- optional alt text and captions
- fit mode (
cover/contain) controls - optional AI-assisted alt text generation
This gives frontend components enough metadata to crop and render correctly without ad-hoc per-component rules.
2) Query a stable image payload shape
IMAGE_GROQ_PARTIAL standardises image fields across queries:
idfromasset._refpreviewfromasset->metadata.lqip- filename and fit mode
- inherited crop/hotspot/alt/caption fields via spread
A stable payload shape keeps rendering components simple and reusable.
3) Render through shared primitives
BaseImage routes Sanity-backed images through <SanityImage /> and falls back to native <img> for plain URLs.
SanityImage then:
- injects Sanity project/dataset automatically
- sanitises crop/hotspot values
- requires
sizesfor responsive optimisation (warns when missing) - supports mode-aware rendering (
coverorcontain)
This consolidates optimisation logic in one place instead of duplicating it across blocks.
4) Optimise delivery URLs
urlFor(...) and urlForImage(...) build transformed Sanity CDN URLs with settings such as:
auto('format')for modern format negotiationfit('max')to avoid unnecessary over-cropping- targeted dimensions for Open Graph and favicon assets
The result is smaller payloads with predictable dimensions for metadata surfaces.
Implementation touchpoints in page components
Performance-sensitive components pass explicit sizes values based on layout intent, for example:
- full-bleed backgrounds use
sizes="100vw" - split layouts use breakpoint-aware
sizesexpressions - above-the-fold hero media can use
loading="eager"deliberately
These choices ensure browsers fetch the right asset size for each viewport rather than downloading oversized images.
Practical implementation guidance
- Always include image partial fields in GROQ when adding a new visual block.
- Always pass a
sizesprop when usingSanityImageorBaseImage. - Keep fit decisions (
covervscontain) in content model or block config, not hardcoded in multiple components. - Use URL helper utilities for SEO images instead of constructing strings manually.
- Treat preview/LQIP as progressive enhancement, not a substitute for correct responsive sizing.
Operational benefits
- Lower image transfer sizes and faster paint times.
- Consistent rendering rules across modular blocks and Portable Text.
- Better accessibility via centralised alt text support.
- Fewer media regressions because query, schema, and render layers share one contract.
Last updated: 27 Apr 2026, 14:59:48
Caching and Invalidation
Use layered cache tags and webhook-driven revalidation to keep page, redirect, feed, and manifest data fast while staying fresh after CMS changes.
Editorial Workflow
Follow the end-to-end editorial lifecycle in the starter, from document creation and localisation to review states and publication in Sanity Studio.
