Setting default slug and making it readOnly in Sanity.io

17 replies
Last updated: Jun 22, 2020
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.

i'm guessing you could set the initialValue of the slug.

https://www.sanity.io/docs/initial-value-templates
default or initial values can be for documents, not objects. The guide below provides some examples of how you can can programmatically set initial values for fields. You can do the same for a slug!
https://www.sanity.io/guides/getting-started-with-initial-values-for-new-documents

readOnly
is also a field you can define on the schema definition of your document. You'll need to add this field specifically to your slug field that you want to be initialized.
Thanks
user J


user N
tried the
initialValue
situation and didn’t work 😞
user J
interesting enough seems like initial values just aren’t working for me at all…hmm
paste your schema definition!
export default {
  name: 'blog',
  title: 'Blog',
  type: 'document',
  initialValue: {
    secondaryTitle: true,
  },
  fields: [
    { title: 'SEO', name: 'seo', type: 'pageMeta' },
    { title: 'Page Header', name: 'simpleHero', type: 'simpleHero' },
    {
      name: 'slug',
      title: 'Slug',
      type: 'slug',
    },
    {
      name: 'secondaryTitle',
      type: 'boolean',
      title: 'Secondary Title',
    },
    {
      title: 'Featured Article',
      name: 'featured',
      type: 'reference',
      to: [{ type: 'post' }],
    },
    {
      title: 'Featured Categories',
      name: 'categories',
      type: 'array',
      of: [{ type: 'reference', to: [{ type: 'category' }] }],
      validation: (Rule) => Rule.required(),
    },
  ],
  preview: {
    select: {
      title: 'simpleHero.title',
    },
  },
};
user J
Hmmm, I think you have this correct. So...1. when creating a new blog document using studio, is the secondaryTitle boolean set to true?
2. are you sure you're checking the right studio deployment?
3. are you getting errors from something else in the schema?
I'm sorry I can't be more helpful 😞
user J
more than helpful! Thank you. I’m going to double check those things.
Ohhh maybe thats the issue. this is a single document
if you've already created the document the initial won't work! haha yeah it only works for newly created docs
ahhh okay that actually makes a ton of sense hahah
I was reading
initialValue
as the first time it’d have a value…not the first time it would be created. But it makes sense.
what is the best way to set
initialValue
for slug? Is it possible at all?
what is the best way to set 
initialValue
 for slug? Is it possible at all?
Patrick Johnson
turns out you can set the
initialValue
of slug by defining it as an object:

initialValue: () => ({
    slug: {_type: "slug", current: `${randomstring.generate({length: 12, charset: 'alphabetic'})}-${Date.now().toString(16).toUpperCase()}`}
  })

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?