Initial value not applied when editing existing documents in Sanity

2 replies
Last updated: Oct 20, 2024
Puzzled on how to set
intialValue
for a field. Docs seem pretty straightforward but when I use the example similar to
export default {
  name: 'project',
  type: 'document',
  title: 'Project',
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: 'string',
      initialValue: 'This string'
    },
It does not fill in the filed with the initial value, nothing happens at all, no error in the console or anything. What am I missing? example taken from
here
AI Update

Based on the Sanity documentation, you're experiencing a common issue with initialValue in Sanity Studio. Here's what's likely happening and how to fix it:

The Most Common Issue: Existing Documents

Field-level initialValue only works when creating NEW documents. If you've already created the document (even as a draft), the initial value won't populate. This is the most frequent reason developers think initialValue isn't working.

Solution: Delete any existing drafts/documents and create a completely new document to see the initial value.

Your Code Looks Correct

The example you're using is valid for Sanity Studio v3:

export default {
  name: 'project',
  type: 'document',
  title: 'Project',
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: 'string',
      initialValue: 'This string'
    }
  ]
}

Better Approach: Initial Value Templates

For Sanity Studio v3, the recommended approach is using Initial Value Templates instead of field-level initialValue. These are more powerful and reliable:

// sanity.config.ts
export default defineConfig({
  // ... other config
  document: {
    newDocumentOptions: (prev, context) => {
      return [
        ...prev,
        {
          id: 'project-with-title',
          title: 'Project',
          schemaType: 'project',
          value: {
            title: 'This string'
          }
        }
      ]
    }
  }
})

Additional Troubleshooting

  1. Use defineField helper for better type safety:
import {defineField, defineType} from 'sanity'

export default defineType({
  name: 'project',
  type: 'document',
  fields: [
    defineField({
      name: 'title',
      type: 'string',
      initialValue: 'This string'
    })
  ]
})
  1. Clear cache: Clear browser cache/local storage and restart your dev server
  2. Verify Studio version: Make sure you're on Studio v3 (check package.json)
  3. Check console: Look for any errors in the browser developer console

The key takeaway: initialValue at the field level does work in Sanity v3, but only for brand new documents. For more control and reliability, Initial Value Templates are the modern recommended approach.

Are you creating a new document to test it? Initial values are only applied when you initially create the document (or create the object, in the case of array items).
Existing documents won't get the initial value applied.
Oh jeez, you know that got me once before a while back and as soon as I saw your response I knew it. Yep that was it, I was just refreshing that page. Thanks for the reminder 🤦

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?