Shared field value across all documents of same type

2 replies
Last updated: Mar 27, 2024
Hey everyone!
Does anyone know if it is possible to create a shared field for all documents of the same type. The idea would be that the user can input a custom value to field X for example in the first document and that value would then be shared by ALL documents of the same type.
AI Update

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:

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 thread
2 replies
👋 I’d put that field in a document of its own. You could then either put it in a reference field in the documents that need to use it for easy editor access, or since you know that all documents are going to reference it you can leave it out entirely and just handle stitching it together with GROQ later.
Yeah or in a settings document. You could also create an action that updates the other documents on publish. So you update the field in one doc, click publish, and it patches the other docs.

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?