How to set a default slug and make it readOnly in Sanity?

17 replies
Last updated: Jan 28, 2026
Is there a way to set a default slug and make it
readOnly
?
AI Update

Yes! You can set a default value for a slug field and make it read-only using field-level properties in your schema definition.

Using initialValue and readOnly

The most straightforward approach is to use the initialValue property directly on your slug field along with the readOnly property:

defineField({
  name: 'slug',
  type: 'slug',
  initialValue: {
    current: 'my-default-slug'
  },
  readOnly: true
})

Note that slug fields store their value in a current property, so your initialValue needs to be an object with that structure.

Conditional readOnly

If you want the slug to be editable initially but become read-only after it's set (a common pattern to prevent breaking URLs after publishing), you can use conditional fields:

defineField({
  name: 'slug',
  type: 'slug',
  initialValue: {
    current: 'my-default-slug'
  },
  readOnly: ({value}) => !!value?.current
})

This makes the field read-only once it has a value, but allows editing when it's empty. The readOnly property accepts a callback function that receives parameters like value, parent, document, and currentUser, giving you full control over when the field should be locked.

For example, you could make it read-only based on document state:

readOnly: ({document}) => document?._id !== undefined

Or based on user roles:

readOnly: ({currentUser}) => !currentUser?.roles.find(role => role.name === 'admin')

The combination of field-level initialValue and conditional readOnly provides flexible control over your slug fields without requiring additional configuration.

Show original thread
17 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.

Was this answer helpful?