Difficulty in passing initial values to singletons in Structure Builder.

8 replies
Last updated: Aug 24, 2023
Is there a way to pass ininitalvalues / initialvaluetemplates to singletons in structurebuilder ?

 S.document()
        .schemaType(schemaTypeName)
        .documentId(docId)
        .initialValueTemplate(templateId)
Doing the above doesn’t work
AI Update

Unfortunately, initialValueTemplate() doesn't work with singleton documents in Structure Builder. This is a known limitation - when you use S.document().documentId() to create a singleton, the initial value template functionality is bypassed because you're pointing directly to a specific document ID rather than creating new documents from a template.

The core issue is that initial value templates are designed for creating new documents, but singletons by definition only exist once and are referenced by a fixed document ID. The template system never gets invoked because you're not going through the document creation flow.

Workarounds

Here are a few approaches to get initial values into your singleton:

1. Manually create the document first

Before adding the singleton to your structure, temporarily create it the normal way (through the "New document" menu) so the initial value template can apply. Then add it to your structure builder configuration.

2. Use default values in your schema

Instead of relying on initial value templates, define initialValue directly in your schema fields:

defineField({
  name: 'myField',
  type: 'string',
  initialValue: 'default value'
})

3. Create the document programmatically

Use the Sanity client to create the singleton document with your desired initial values:

import {client} from './sanityClient'

client.createIfNotExists({
  _id: 'settings',
  _type: 'settings',
  // your initial values here
  title: 'Site Settings',
  // ...
})

4. Use a migration

If you need to initialize or reset singleton values, you can write a Sanity migration to set the values programmatically.

The schema-level initialValue approach (option 2) is generally the most straightforward for singletons since it keeps your default values close to your schema definition and doesn't require manual document creation steps. As noted in the singleton document guide, you need to create the singleton document before adding it to your configuration, so combining option 1 (creating it first with a template) or option 3 (programmatic creation) with proper schema defaults gives you the best control over initial values.

Should be possible according to the docs . Have you followed the steps there?
This shows how to do it for documentTypeList. But I’m creating a singleton (document), so I’m never clicking on the “Create new” button.I.e. “initialValueTemplates” is not available on document, only on documentList/documentTypeList
I have a schematype which is being used in a fixed amount of places (e.g. 10). Where I have hardcoded the id. I just want the ability to pass in a set of initial values for each of the instances I’m creating.
The code that you provided earlier seems to work in my test.Can you check the following:
1. Value for
schemaTypeName
is correct and matches the schema name2. Value for
docId
is correct and matches the type of the document matches
schemaTypeName
3. Value for
templateId
is correct and the
schemaType
specified there matches the one in (1)4. Delete the existing singleton document and see if the new one has the initial values
hm, how do I delete a singleton though ?
Are you unable to delete it from the menu?
No sorry you’re right. That was an issue on my side.
Deleting automatically recreates a doc with the initial values
🙂
thanks 🙂

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?