Shared field value across all documents of same type
I understand what you're looking for, but Sanity doesn't have a built-in feature for truly "shared" fields where editing one document automatically updates that field value across all other documents of the same type. Each document in Sanity is independent and maintains its own field values.
However, there are several approaches you could take depending on your exact use case:
1. Use a Reference to a Single Source Document (recommended)
The most "Sanity-like" approach is to create a separate document type that holds the shared value, then reference it from all your documents:
// schemas/sharedConfig.ts
defineType({
name: 'sharedConfig',
type: 'document',
title: 'Shared Configuration',
fields: [
defineField({
name: 'fieldX',
type: 'string',
title: 'Shared Field X'
})
]
})
// schemas/myDocType.ts
defineType({
name: 'myDocType',
type: 'document',
fields: [
defineField({
name: 'sharedConfig',
type: 'reference',
to: [{type: 'sharedConfig'}]
})
// ... other fields
]
})Then when querying, you can use GROQ to dereference and get the shared value:
*[_type == "myDocType"] {
...,
"sharedFieldX": sharedConfig->fieldX
}This way, updating the single sharedConfig document updates the value for all documents that reference it. This is the cleanest solution for truly shared values in Sanity's content model.
2. Use Initial Value Templates (for setting defaults on new documents)
If you want new documents to start with a specific value that was set in a previous document, you could use Initial Value Templates. These let you dynamically set initial values when creating new documents by configuring them in document.newDocumentOptions:
// sanity.config.ts
export default defineConfig({
// ... other config
document: {
newDocumentOptions: (prev, context) => [
...prev,
{
id: 'myDocType-with-preset',
title: 'My Document',
schemaType: 'myDocType',
value: async (params, context) => {
const client = context.getClient({apiVersion: '2024-11-01'})
// Fetch the most recent document's field value
const latest = await client.fetch(
`*[_type == "myDocType"] | order(_createdAt desc)[0].fieldX`
)
return {
fieldX: latest
}
}
}
]
}
})This copies the value to new documents, but they remain independent afterward - changes to one won't affect others.
3. Field-Level Initial Values (for simple static defaults)
For simple default values that don't need to be dynamic, you can use field-level initialValue:
defineField({
name: 'fieldX',
type: 'string',
initialValue: 'some default value'
})4. Programmatic Updates via Sanity Functions
If you truly need to replicate a value across all documents when one changes, you could write a Sanity Function that listens to document mutations and updates all other documents. However, this creates data redundancy and potential sync issues, so it's generally not recommended.
The reference approach (#1) is by far the cleanest solution for shared values in Sanity. It maintains a single source of truth, avoids data duplication, and leverages Sanity's native reference system. What's your specific use case? That might help determine which approach would work best for your needs.
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.