Set up Draft Previews with Gatsby Cloud Hosting

3 replies
Last updated: Aug 26, 2020
Hi guys, I am working on one gatsby-sanity-netlify stack at this moment on one of my projects. But, I am having some questions to ask, can anyone suggest, how to implement live web preview in sanity for new blog posts, I know it's working for existing posts or published posts, for which pages are already generated in gatsby, but as a content editor one needs to see live web preview for the unpublished posts as well. If anyone have gone through this in past & have some solutions then kindly help me here..
AI Update

Great question! This is a common challenge with Gatsby and Sanity. The issue is that Gatsby generates pages at build time, so there's no page yet for unpublished content. Here's how to solve it:

The Core Solution: Gatsby Preview + overlayDrafts

You'll need to set up a Gatsby Preview instance that runs in development mode. Here's the approach:

1. Configure gatsby-source-sanity for Drafts

In your gatsby-config.js, enable draft overlays:

{
  resolve: 'gatsby-source-sanity',
  options: {
    projectId: 'your-project-id',
    dataset: 'your-dataset',
    token: process.env.SANITY_READ_TOKEN, // Required for drafts
    watchMode: true, // Real-time updates in dev
    overlayDrafts: true, // Show drafts instead of published
  }
}

The overlayDrafts: true option is crucial - it overlays draft versions over published content, and includes draft-only documents that haven't been published yet.

2. Generate Preview Pages for All Drafts

You'll need to modify your gatsby-node.js to create pages for all documents, including drafts. Query for both published and draft content:

const result = await graphql(`
  {
    allSanityPost {
      nodes {
        _id
        slug {
          current
        }
      }
    }
  }
`)

With overlayDrafts: true, this query will include draft-only posts that haven't been published yet.

3. Set Up Sanity Preview URL

In your Sanity Studio, configure the preview URL using resolveProductionUrl in your desk structure:

S.document().views([
  S.view.form(),
  S.view.component(Iframe)
    .options({
      url: (doc) => 
        `https://your-preview-site.netlify.app/preview/${doc.slug.current}`
    })
    .title('Preview')
])

4. Deploy a Persistent Preview Environment

Here's the key: deploy a separate Gatsby instance on Netlify that runs in development mode (or runs frequent rebuilds). This can be:

  • A Netlify branch deploy that rebuilds on webhook triggers
  • Gatsby Cloud Preview (if you're using Gatsby Cloud)
  • A long-running development server on a service like Heroku

Set up a Sanity webhook to trigger rebuilds when content changes, so new draft posts generate pages.

Alternative: Client-Side Preview Rendering

For truly instant previews without waiting for rebuilds, you could implement a client-side preview route that fetches and renders draft content directly from Sanity's API, bypassing Gatsby's static generation. This requires more custom code but provides immediate previews.

The watchMode: true option enables real-time updates during development, so changes in Studio appear instantly in your preview environment without manual rebuilds.

The combination of overlayDrafts and a dedicated preview environment is the standard approach for previewing unpublished content in Gatsby + Sanity setups!

Show original thread
3 replies
You'll need to run the live preview server somewhere. The easiest way is to do so in Gatsby Cloud, or you can self host it:
https://sanity-io-land.slack.com/archives/C9YQ161LZ/p1597705361144400
https://www.simeongriggs.dev/roll-your-own-gatsby-live-preview-for-sanity
https://medium.com/the-couch/live-preview-in-gatsby-without-the-cost-21f8ac0337bb
https://www.sanity.io/blog/get-started-with-gatsby-and-structured-content
https://www.phmu.de/blog/real-time-preview-for-gatsby-websites-with-the-sanity-io-headless-cms/ The key here is to set up the plugin to have
overlayDrafts: true
 and provide it a token with read permissions in the preview environment, so that the listeners can access the drafts.
Thanks a lot
user Y
, I will take a look on all the links that you have shared, I really appreciate your quick help & response..
No problem! I'm sure you'd find even more pointers by searching this slack btw! And do join the gatsby channel as well

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?