Update custom document badge with custom document action
No, you cannot directly update or query custom document badges from a custom document action. This is an important architectural limitation to understand.
Why This Limitation Exists
Document badges exist purely in the Studio UI layer and are not persisted as part of the actual document data. According to the Document Badges documentation, badge values are not written to the document itself - they're computed and displayed on-the-fly based on the document's current state.
What Document Badges Can Access
When you create a custom badge, it receives these properties:
id- The document IDtype- The schema typedraft- The draft document (if any)published- The published document (if any)
Badges read from the document state and display information, but they don't store queryable data themselves.
Alternative Approaches
If you need to track status or metadata that both document actions and badges can access, consider these patterns:
- Store status in the document itself - Add a field to your schema (like
statusorworkflowState) that both your custom action and badge can read from - Use document actions to update document fields - Your custom action can modify document fields using the
useDocumentOperationhook - Have badges display document field values - Your badge can read from those fields and display them visually
Here's a simple example pattern:
// In your schema
{
name: 'myDocument',
type: 'document',
fields: [
{
name: 'status',
type: 'string',
options: {
list: ['draft', 'review', 'approved']
}
}
]
}
// Custom badge reads the status
export function StatusBadge(props) {
const status = props.published?.status || props.draft?.status
return {
label: status?.toUpperCase(),
color: status === 'approved' ? 'success' : 'default'
}
}
// Custom action updates the status
export function ApproveAction(props) {
// Use useDocumentOperation to patch the document
// and update the status field
}This way, your document action modifies the actual document data, and your badge displays that data - creating a workflow where both features work together through the document's content rather than trying to communicate directly.
Show original thread2 replies
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.