Unlock seamless workflows and faster delivery with our latest releases – get the details

Strategies for saving changes as drafts in Sanity Studio for previewing in development environment.

4 replies
Last updated: Apr 22, 2020
Curious, what are some of your guys strategies for saving some changes in the studio as a draft, and thus make them visible in a site preview build, but not published to the live one?
Apr 21, 2020, 2:39 PM
Hi Igor, are you looking to save a newly created document as a draft via Sanity’s HTTP API, for example? In that case you could prefix the document’s
_id
with
drafts.
as described over here: https://www.sanity.io/docs/http-mutations#create-c732f27330a4
Let me know if you were looking for something different
🙂
Apr 21, 2020, 5:46 PM
hi
user M
. Not sure this is quite it. For context, my use case is with gatsby.
This solution you pointed to, this is just for articles that have not yet been published right? What would be ideal I think is if I had some way to "publish" pages to a draft status, that a front-end could either build or not build, based on some flags set for prod or preview environments.

The issue to me really is that I don't know of a way to check what a page looks like in the front end without publishing the content. This leads to unfinished work being "published", and the hazard that it may get pushed out along with some other change that we want to push live
Apr 21, 2020, 7:37 PM
Thanks for the extra context! Indeed, what I suggested is just for when you create new documents using the API.
If I understand correctly, what you’re essentially looking for is a way to preview drafts in development and only published documents in production. When you make changes to a document in the Sanity Studio, these changes are saved in the draft version of the document until you hit “Publish”.

Assuming you’re using `gatsby-source-sanity`: have you tried adding a
token
and setting
overlayDrafts
to true? This will let you ‘overlay’ the draft version over the regular version of the document in development. If you want it to update on every keypress, you can also set
watchMode
to true.
The auth
token
you can create via manage.sanity.io > Your project > Settings > API. You’ll want to create a token with Read permissions in this case. Here is some additional information on keeping tokens secure, just in case: https://www.sanity.io/docs/keeping-your-data-safe
By settings things up this way, you should be able to preview drafts in development while only using published documents (and not drafts) in production.
Apr 21, 2020, 9:50 PM
Sounds like an intriguing option, thanks
user M
, going to try it out!
Apr 22, 2020, 6:55 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?