Discussion about signaling published/unpublished documents with an icon in Sanity.io

14 replies
Last updated: Jul 6, 2021
Another question about published/unpublished documents. Is there a way to signal to the users that a given document is un/published? It would be really helpful to include an icon for that in the document list.
Jul 5, 2021, 2:02 PM
About halfway down this page it shows one way you can do that: https://www.sanity.io/docs/previews-list-views
Jul 5, 2021, 2:24 PM
Cool, but this is not specifically for un/published statuses right?
Jul 5, 2021, 3:14 PM
The
_id
of a document begins with
drafts
if it is not published. So, this works:
preview: {
        select: {
            title: 'title',
            theId: '_id'
        },
        prepare(selection) {
            const {title, theId} = selection
            const isDraft = theId.substring(0,5) == 'draft' ? '👷' : '✅'
            return {
                title: title,
                media: <span style={{fontSize: '1.5rem'}}>{isDraft}</span>
            }
        }
    }
which renders:
Jul 6, 2021, 12:32 PM
Cool! I’ll try it, thanks
Jul 6, 2021, 12:32 PM
Where do did you get the icons from?
Jul 6, 2021, 12:38 PM
So technically, a “draft” state can mean three things: 1. you have modified an existing document (that still can be “live”), 2. you have created a new one or 3. you have unpublished it, correct?
Jul 6, 2021, 12:49 PM
A document, identified by
_id
is either in a published or unpublished state. If it's not published the
_id
will start with `drafts.`(and it won't be exposed in api)
What is immediately visible in the document editor (and exposed in API if published) is identified by the
_id
and a revision, under
_rev
. As you change among various historical versions of your document, the
_rev
changes for each.
Jul 6, 2021, 1:56 PM
Oh so if you start and modify an already published document, that document will be unpublished automatically?
Jul 6, 2021, 1:57 PM
No, but you will start a new revision.
Jul 6, 2021, 1:58 PM
There are two places you can click to see this in action...go to a document and fiddle around - publish, unpublish, edit, go up to "current version" and choose a prior revision. As you do so, go each time to "inspect" and you can see the
_rev
and
_id
values changing
Jul 6, 2021, 1:59 PM
Ok, I see. So there is no direct property to check for only published/unpublished
Jul 6, 2021, 2:00 PM
Not that I know of...but I'm not an authority on the matter!
Jul 6, 2021, 2:00 PM
Yep, gotcha
Jul 6, 2021, 2:00 PM
But I think that's the case since in the docs:

Drafts are not available to the public API, but can be accessed using an authenticated client or using an access token. Sometimes you therefore might need to filter drafts from your queries. You can do this by adding the following filter:
*[!(_id in path('drafts.**'))] // _id matches anything that is *not* in the drafts-path

https://www.sanity.io/docs/drafts
Jul 6, 2021, 2:04 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the 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?