How to resolve references in Sanity preview context?

4 replies
Last updated: Jun 8, 2020
One more question! I have a preview thingie based on this: https://www.sanity.io/docs/preview-content-on-site Now I want to consult a field on a reference (e.g.
document.property->nestedPoperty
), but the object passed in just contains the reference. How can I resolve the reference in this context?
AI Update

Based on the documentation you're referencing about preview content, when you're working with references in Sanity's visual editing setup, you'll need to dereference and expand references in your GROQ query rather than trying to resolve them client-side in the preview object.

The Solution: Use GROQ Projections

When fetching data for preview, you need to dereference references using the -> operator in your GROQ query:

*[_type == "yourDocumentType"] {
  _id,
  property,
  "nestedProperty": property->nestedProperty
}

Or if you want the entire referenced document:

*[_type == "yourDocumentType"] {
  _id,
  property,
  propertyExpanded: property->
}

Why This Matters for Visual Editing

The preview content passed to your components needs to have references already resolved because:

  1. Content Source Maps need to track the full path through dereferenced content for click-to-edit functionality
  2. The preview system doesn't have a built-in client-side reference resolver (no separate ReferenceResolver API)
  3. Your queries should return the complete data shape your components expect

Example Pattern

If your document structure is:

{
  _type: "page",
  author: {
    _type: "reference",
    _ref: "author-123"
  }
}

Your GROQ query for preview should be:

*[_type == "page"] {
  _id,
  title,
  "authorName": author->name,
  "authorBio": author->bio,
  // Or get the whole author object:
  authorFull: author->
}

For Visual Editing Specifically

When using the Presentation tool or fetching content for visual editing, make sure your data-fetching functions include these projections. The dereferencing happens at query time on Sanity's CDN, not in your preview components.

If you're using @sanity/client with useLiveQuery or similar hooks, the dereferenced data will flow through automatically to your components with proper Content Source Maps for the visual editing overlays.

The key takeaway: resolve references in your GROQ queries using ->, not in your component code. The preview object you receive should already have the nested data you need. This is the standard pattern for working with references in Sanity - there's no separate client-side resolver API needed.

Show original thread
4 replies
I think you will need to query that data async, using the client.
something along the lines of:

import client from 'part:@sanity/base/client'

export default async function resolveProductionUrl(document) {

  const referencedDoc = await client.getDocument(document.property._ ref)

  return `<https://my-site.com/posts/${referencedDoc.property}>`
}
Perfect, thanks again!
Perfect, thanks again!
Does this actually work? https://github.com/sanity-io/sanity/issues/1374 and sanity ticket 827

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?