Using `walkjs` to fetch and attach image metadata in Sanity queries
Unfortunately, there isn't a built-in way to automatically include blurhash in every query without explicitly requesting it in GROQ. However, there are a few approaches that can help minimize the manual work:
The Schema-Level Solution
The good news is that blurhash is included by default when you upload images to Sanity. According to the image metadata documentation, you don't need to explicitly configure it in your schema - it's automatically extracted and stored alongside your image assets.
This means the data is already there in your Content Lake, you just need to query for it.
Making GROQ Queries Easier
While you still need to explicitly request the blurhash in GROQ, you can make this less repetitive:
1. Create a GROQ fragment/helper:
// Define once in a constants file
const IMAGE_WITH_METADATA = `{
asset->{
_id,
url,
metadata {
dimensions,
lqip,
blurHash
}
}
}`
// Use in queries
const query = `*[_type == "post"] {
title,
mainImage ${IMAGE_WITH_METADATA}
}`2. Use a helper function with your SDK:
// Wrapper that automatically adds image metadata
function queryWithImageMetadata(baseQuery) {
// Parse and inject image metadata projections
// This is more complex but possible
}3. Consider using sanity-codegen or TypeGen:
With proper TypeScript types, you can create reusable projection types that make it easier to consistently query the same fields.
Why No Auto-Include?
GROQ's philosophy is explicit projections - you only get what you ask for. This keeps payloads small and queries performant. While it means a bit more typing, it prevents accidentally fetching large amounts of metadata you might not need in every context.
The tradeoff is that you need to be explicit in your projections, but the benefit is that your schema configuration ensures the data is always available when you do query for it.
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.