🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Debugging inconsistency with Vision Plugin and draft titles in Sanity.io

14 replies
Last updated: Aug 20, 2020
Hi! Can someone help me debug a strange inconsistency I'm encountering?Using the Vision Plugin, I cannot get the draft for one type of document, but I can for other types.
This query *[_type == "page" && slug.current == "about"][0].title doesn't get me the draft title, only the published title.
But this query *[_type == "event" && slug.current == "meet-up"][0].title does gives me the draft title, as expected
So puzzling
🤔
Aug 19, 2020, 6:37 PM
That’s very puzzling…. Just tested against my local copy of the movie data set and Vision is pulling my draft title… when you run
*[_type == "page" && slug.current == "about"][0]
do you get the draft id?
Aug 19, 2020, 7:00 PM
yes, it also works fine for me with the movie dataset.But not for this project.
No, I don't get the draft id.
Aug 19, 2020, 7:10 PM
Checking a quick assumption: does your Page document have
liveEdit: true
?
Aug 19, 2020, 7:12 PM
no liveEdit
Aug 19, 2020, 7:33 PM
this is the document JS file:

import { FaCopy } from 'react-icons/fa'

import conditionalFields from '../../components/conditionalFieldsCustomInputComponent'

export default {
  name: 'page',
  title: 'Seiten',
  type: 'document',
  icon: FaCopy,
  fieldsets: [
    {
      title: 'Banner-Bild auf Mobile',
      name: 'mobile',
      options: { collapsible: true },
    },
  ],
  fields: [
    {
      name: 'title',
      title: 'Titel',
      type: 'string',
      validation: (Rule) => Rule.required().error('Pflichtfeld'),
    },
    {
      name: 'body',
      type: 'object',
      inputComponent: conditionalFields,
      fields: [
        {
          name: 'richText',
          title: 'Body',
          type: 'blockContent',
        },
      ],
    },
    {
      title: 'Desktop Banner-Bild',
      name: 'heroImage',
      type: 'image',
      description: 'Bildschirmbreite einnehmendes Bild',
      validation: (Rule) => Rule.required().error('Pflichtfeld'),
      options: {
        hotspot: true,
        metadata: ['lqip'],
      },
      fields: [
        {
          name: 'alt',
          type: 'string',
          title: 'Alternativer Text',
          validation: (Rule) => Rule.required().error('Pflichtfeld'),
          description:
            'Bildbeschreibung für Barrierefreiheit und Suchmaschinenoptimierung',
          options: {
            isHighlighted: true,
          },
        },
      ],
    },
    {
      title: 'Mobile Banner-Bild',
      name: 'mobileHeroImage',
      type: 'image',
      description: 'Bildschirmbreite einnehmendes Bild',
      fieldset: 'mobile',
      options: {
        hotspot: true,
        metadata: ['lqip'],
      },
    },
    {
      name: 'slug',
      type: 'slug',
      title: 'Titelform in URL',
      description: 'Für die Home-Seite nur einen Schrägstrich erfassen.',
      validation: (Rule) => Rule.required().error('Pflichtfeld'),
      options: {
        source: 'title',
        maxLength: 96,
      },
    },
    {
      name: 'description',
      title: 'Kurze Beschreibung von der Seite',
      description: 'Wird im Suchresultat von Google angezeigt.',
      validation: (Rule) => [
        Rule.max(300).warning(
          'Die Beschreibung sollte kürzer als 300 Zeichen sein'
        ),
      ],
      type: 'text',
      rows: 1,
    },
  ],
}
Aug 19, 2020, 7:34 PM
This is bugging me…. i brought your schema into my local project and it’s not grabbing the draft… i reduced it to a test case of just title and slug… it’s not anything i can think of. I’ll poke a couple people and see if i can get it figured out
Aug 19, 2020, 7:51 PM
It’s gonna bug me until i figure it out
Aug 19, 2020, 7:52 PM
yeah, I was also going to remove fields until it works. so even with only a title and a slug, it doesn’t work?let me know if you need that I invite you into the project.
Aug 19, 2020, 7:57 PM
Just title and slug. In one of my local testing studios...
Aug 19, 2020, 8:03 PM
how strange 😮
Aug 19, 2020, 8:05 PM
(if I edit the slug to “about-2” and fetch by slug.current == “about-2". I do get the draft)
Aug 19, 2020, 8:12 PM
Got to the root of this,
user E
! by calling
[0]
you’re only getting the first item in that array. If you look at the documents in the result, you should see 2 documents. The default ordering is alphanumeric 0-9a-z.
In my
movie
dataset, all my movie documents have an id that starts with
movie
and drafts start with
drafts.movie
so the draft was always first. There’s a chance the randomly generated _id strings could result in drafts coming first, but often they won’t. So if you want the draft version of the doc, you’ll need to check to see if the id starts with draft
Aug 20, 2020, 1:20 PM
Oh! Thanks for figuring that out! 😃That's why I was also always getting the draft first in the movie dataset.
Aug 20, 2020, 2:28 PM
I'm going to change my query to this in order to be sure to always get the draft first if there's one:*[_type == "page" && slug.current == $slug] | order(_updatedAt desc)[0]
Aug 20, 2020, 4:04 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?