The case of the disappearing blog posts: how one "+" button hid my content in Sanity

Some of my blog posts were saved in Sanity but showed up nowhere: not on the site, not in the Studio. The cause wasn't a network error or a race condition. It was one item in a "create" menu.

Sanity

Next.js

Debugging

i18n

CMS

The symptom

I write my blog in Sanity Studio, which is embedded in my Next.js portfolio. Every post has an English and a Polish version, handled by the @sanity/document-internationalization plugin.

Every now and then, a post I had just written would disappear. I filled in every field, validation passed, and I hit Publish. Then the post wasn't on the site, and it wasn't in the Studio's post lists either. So I wrote it again, and the second time it worked.

Intermittent bugs are the worst. My first guesses were the usual ones: an unawaited promise, a double submit, a swallowed error from the API.

Step one: the code that saves posts

I went looking for the save logic: client.create, patch, mutate, asset uploads. There wasn't any. My app never writes to Sanity. The Studio saves documents itself, including validation, autosave, image uploads and publishing.

That ruled out a whole class of bugs. If the Studio says a document is saved, it's saved. So the real question became: is the post actually missing, or just invisible?

Step two: asking the data directly

I ran one read-only GROQ query against the dataset:

*[_type == "post"]{
  _id,
  language,
  "slug": slug.current,
  publishedAt
}

The answer was right there. Four published posts had language: null. Three of them had a twin with the same slug and language: "pl", created minutes, hours or days later. Those twins were my "second attempts".

The posts had never been lost. They were saved without a language.

Why a post without a language is invisible

My Studio sidebar groups posts by language:

S.documentList()
  .title(`Blog Posts (${language.title})`)
  .filter(`_type == "post" && language == "${language.id}"`)

The site queries do the same with language == $locale. And the generic "all documents" list deliberately leaves posts out, to avoid duplicates.

So a post with language: null matches no list and no query. On top of that, the language field was hidden and read-only in the form, so I had no way to notice.

How the language went missing: the "+" menu

This was the most interesting part. The internationalization plugin registers templates such as "English Blog Post" (post-en) and "Polski Blog Post" (post-pl). Sanity also creates a default template for every document type: plain "Blog Post" (post), which sets no language.

My filtered per-language lists didn't say which templates they allow. So Sanity worked out the type from the filter and offered every template for post. Clicking "+" in "Blog Posts (Polski)" showed:

  1. Blog Post: listed first, no language
  2. English Blog Post
  3. Polski Blog Post

I did have a newDocumentOptions filter in my config, but it only covered the global "Create" button:

newDocumentOptions: (prev, {creationContext}) => {
  if (creationContext.type !== 'global') {
    return prev
  }
  // ...
}

That explains why it was intermittent. When I clicked "Polski Blog Post", everything worked. When I clicked the first, most natural option, the post quietly ended up nowhere.

A bonus find: config that did nothing

My sanity.config.ts also had a templates key at the top level, defining its own language templates. Sanity only reads templates from schema.templates, so that code never ran. It wouldn't have worked under schema either: its IDs were the same as the plugin's (Sanity throws an error on duplicate template IDs), and templates with parameters don't appear in "+" menus anyway.

A second, separate issue: caching

The site reads posts through unstable_cache with revalidate: 3600 and cache tags. But nothing ever called revalidateTag, so a newly published post could take up to an hour to appear. That's a second source of "sometimes it works", completely separate from the first.

The fixes

1. Each list creates documents in its own language only:

S.documentList()
  .schemaType(schemaType)
  .filter('_type == $type && language == $language')
  .params({type: schemaType, language: language.id})
  .initialValueTemplates([
    S.initialValueTemplateItem(`${schemaType}-${language.id}`),
  ])

2. A backstop in newDocumentOptions. Translatable types are only offered with a language template, in every context:

if (creationContext.type === 'global') {
  return option.templateId === `${schemaType}-${DEFAULT_LANGUAGE.id}`
}
return languageTemplateIds.has(option.templateId)

3. language is required, and only visible while it's empty. A post can no longer be published without a language. If one ever slips through, the form shows radio buttons so I can fix it:

readOnly: ({value}) => Boolean(value),
hidden: ({value}) => Boolean(value),
validation: (rule) => rule.required(),

4. A "Missing language" list in the Studio, so nothing can hide again.

5. publishedAt is required to publish. Before, a published post with no date passed validation but never appeared on the site.

6. A webhook endpoint for revalidation. Sanity calls /api/revalidate on publish, the endpoint checks the signature and clears the posts cache tag, and new posts appear straight away.

What I learned

  • "Not saved" and "not visible" are different bugs. Querying the data directly settled it in a minute.
  • Intermittent doesn't have to mean a race condition. Sometimes it just means the person clicked a different option.
  • Hidden fields need validation too. If users can't see a field, they can't notice it's empty.
  • Config can fail silently. A key in the wrong place in sanity.config.ts doesn't throw an error; it just does nothing.
  • Cache tags only help if something calls them.

The best part: I understood why this was happening before changing a single line of code.