Ship & operate · Lesson 12 of 15 · 15 min

Preview drafts before they go live

Switch between the Delivery and Preview APIs with one setting, then give editors a preview site and an Open preview button.

Editors want to see a lesson on the real site before they publish it. That’s what the Preview API and your Preview token are for.

1. Teach the client about preview

Update the top of the client and replace getClient():

src/lib/contentful.ts

import * as contentful from 'contentful';

const usePreview = import.meta.env.CONTENTFUL_USE_PREVIEW === 'true';
function getClient() {
  const space = import.meta.env.CONTENTFUL_SPACE_ID;
  const accessToken = usePreview
    ? import.meta.env.CONTENTFUL_PREVIEW_TOKEN
    : import.meta.env.CONTENTFUL_DELIVERY_TOKEN;

  if (!space || !accessToken) {
    throw new Error('Missing Contentful settings. Check your .env file.');
  }

  return contentful.createClient({
    space,
    accessToken,
    environment: import.meta.env.CONTENTFUL_ENVIRONMENT ?? 'master',
    host: usePreview ? 'preview.contentful.com' : 'cdn.contentful.com',
  }).withoutUnresolvableLinks;
}

One setting now swaps both the token and the host. Everything else in the project stays the same.

2. Try it locally

Add one line to .env:

.env

CONTENTFUL_USE_PREVIEW=true

Restart npm run dev. The terminal now says 3 lesson entries. Your draft lesson is on the homepage and has its own page.

Set it back to false, restart, and the draft is gone again.

3. Make a preview site on Vercel

You don’t need a second project. Vercel builds every branch at its own URL, and environment variables can be set for those branch builds only.

Create a branch:

git checkout -b preview
git push -u origin preview
git checkout main

In Vercel, go to Settings → Environment Variables and add these for the Preview environment only. Untick Production.

Name Value Environment
CONTENTFUL_SPACE_ID Your Space ID Preview
CONTENTFUL_PREVIEW_TOKEN Your Preview token Preview
CONTENTFUL_USE_PREVIEW true Preview

Redeploy the preview branch from the Deployments tab. Vercel gives the branch a stable URL that looks like learn-hub-git-preview-yourname.vercel.app. Open it. The draft lesson is there. Your production URL still hides it.

4. Add the Open preview button

  1. In Contentful, go to Settings → Content preview and add a preview
  2. Name: Preview site
  3. Tick Lesson and enter this URL, with your own preview address:
https://learn-hub-git-preview-yourname.vercel.app/lessons/{entry.fields.slug}/
  1. Save

Open any lesson in Contentful. The sidebar now has an Open preview button that goes straight to that lesson on the preview site.

5. Keep the preview site fresh

The preview site is static too, so it needs its own rebuild trigger.

  1. In Vercel, create a second deploy hook named contentful-preview for the branch preview
  2. In Contentful, add a second webhook named Vercel preview with that URL
  3. Triggers: in the Entry and Asset rows, tick Autosave, Publish, and Unpublish

Autosave fires often while someone types. That’s okay here. When a deploy hook is called again before its last build finishes, Vercel cancels the older build and runs the newest one. The preview is about a minute behind the editor.

Keep drafts private

  • The Preview token exists only in your .env and in Vercel’s Preview environment. Production can’t read drafts even by accident.
  • Vercel protects preview deployments with a login by default (Settings → Deployment Protection). Leave it on. If it’s off, anyone with the URL can read unpublished content.
  • Editors who need the preview site need access to the Vercel project, or a protection bypass you set up for them.