Gatsby Cloud Docs

Getting started with Gatsby Preview and Sanity.io

Learn how to connect Gatsby Preview with Sanity

What are Gatsby Preview and Sanity, and why use them together?

Sanity.io is a headless CMS that content editors can use to edit and publish content. Gatsby Preview allows developers and content editors to preview code and content changes made in their CMS before publishing those changes.

Setting up a Sanity and Gatsby site

First, you’ll need a Gatsby site that’s connected to a Sanity project and its source code needs to live on Github. If you haven’t set that up yet, you can quickly create a new project by selecting one of the Gatsby templates on sanity.io/create.

Best practices for gatsby-config.js settings

Make sure that watchMode and overlayDrafts are set to true in the Sanity plugin options (this is enabled by default in sites from sanity.io/create). With watchMode, Gatsby injects content changes on the fly, without you having to reload the development server, or refresh the browser. This is done via a listener that receives the content changes from Sanity’s real-time backend. In addition, with Sanity, multiple people in the same Sanity studio can make content edits that are instantly reflected on the frontend development server and on Gatsby Preview.

Signing in to Gatsby Preview

Select Sign in with GitHub. You’ll be asked to authorize the Gatsby Preview app with your GitHub account. If you need to request access to one or more repositories, you can click “request access” here or when creating a preview instance.

Creating a preview instance

Once you’ve authenticated with Preview and GitHub, you can create a preview instance from the dashboard/sites/create page.

Gatsby Preview Create a Preview page

Select your organization from the dropdown menu and then select your site using the search bar or the list.

Note: Repositories must contain one Gatsby project. If the project isn’t in the root folder you can add a path to it. For example, /web. configured at their root to be enabled. Gatsby Preview works best with Gatsby version 2.1.0 and higher.

Pick the repository and site you want to use

Add a new site to Preview page with organization drop down and site highlighted

Configuring your Gatsby site to work with Gatsby Preview and Sanity

Select branch and publish directory

You’ll need to select a branch and then indicate the /web publish directory, which is where the gatsby-config.js lives. If you leave the field blank, it defaults to the root of the site.

Note: Setting a webhook is not required for all users

Sanity users don’t need to configure a webhook, so skip this step.

Setting up Environment Variables

An environment variable references a value that can affect how running processes will behave on a computer, for example in staging and production environments. You must save environment variables in Gatsby Preview to authorize your preview instance to pull source data from Sanity.

You will need to add into Gatsby Preview any environment variable required for your app to run, such as deployment or test environment configuration settings.

You will also need to add in the following Gatsby Preview-specific environment variables:

Set an environment variable called SANITY_READ_TOKEN and its value is the token you generate from https://manage.sanity.io/projects/<YourProjectId>/settings/api (Manage -> Settings -> API), and Add New Token. You can double check gatsby-config.js and the plugin entry for gatsby-source-plugin for the variable name. If you find process.env.SANITY_TOKEN you should enter the variable in Gatsby Preview’s configuration as SANITY_TOKEN.

Select Next

Click Next and wait for the first preview instance to be generated. Now, when you change any copy or content in Sanity, Gatsby Preview should be up to date with your changes.

Setup Gatsby Preview widget in Sanity Studio dashboard

Follow the steps in this doc to add the Gatsby Preview widget to the Sanity Studio dashboard. This makes it much easier for content editors to access the preview instance.

Setting up a local dev environment

If you’d also like to develop your site locally, add the SANITY_TOKEN to the .env file.

Create a file called .env in the web folder:

~/PATH_TO_PROJECT/web
cp .env-example .env

Next, create a read token. Go to https://manage.sanity.io/projects/<YourProjectId>/settings/api (Manage -> Settings -> API), and Add New Token. Give it a nice descriptive label, and only read rights. Copy this token to the .env file as SANITY_TOKEN=”TOKEN”

If you use one of the templates from sanity.io/create, you can add a token with read rights to the /web/.env.development.template file and rename it to .env.development to make it work on local development. The env files with your token should not be committed to git.

To load the token into Gatsby, you’ll need to restart the local development server again.