Previewing Content with Draft Mode in Next.js – Secure Previews from Your Headless CMS

Draft Mode in Next.js allows you to preview unpublished content from your headless CMS without rebuilding your site. This article walks through enabling Draft Mode, securing access with a secret token, validating slugs, and dynamically rendering draft content on request.

Draft ModePreview contentHeadless CMSNext.js API routes

~2 min read · Updated Oct 27, 2025

Introduction


Draft Mode in Next.js lets you preview unpublished content from your CMS without triggering a full rebuild. It enables dynamic rendering for statically generated pages, making it ideal for editorial workflows and content previews.


Step 1: Create a Route Handler


Create a file like app/api/draft/route.ts and enable Draft Mode:

import { draftMode } from 'next/headers'

export async function GET(request: Request) {
  const draft = await draftMode()
  draft.enable()
  return new Response('Draft mode is enabled')
}

This sets a cookie that activates Draft Mode. You can test it by visiting /api/draft and checking for the __prerender_bypass cookie in your browser.


Step 2: Secure Access from Your CMS


To securely enable Draft Mode from your CMS:

  1. Generate a secret token shared between your CMS and Next.js
  2. Configure your CMS to use a preview URL like:
https://your-site.com/api/draft?secret=YOUR_TOKEN&slug=/posts/one

Update your route handler to validate the token and slug:

import { draftMode } from 'next/headers'
import { redirect } from 'next/navigation'

export async function GET(request: Request) {
  const { searchParams } = new URL(request.url)
  const secret = searchParams.get('secret')
  const slug = searchParams.get('slug')

  if (secret !== 'MY_SECRET_TOKEN' || !slug) {
    return new Response('Invalid token', { status: 401 })
  }

  const post = await getPostBySlug(slug)
  if (!post) {
    return new Response('Invalid slug', { status: 401 })
  }

  const draft = await draftMode()
  draft.enable()

  redirect(post.slug)
}

Step 3: Render Draft Content


In your page, check if Draft Mode is enabled and fetch the appropriate data:

import { draftMode } from 'next/headers'

async function getData() {
  const { isEnabled } = await draftMode()

  const url = isEnabled
    ? 'https://draft.example.com'
    : 'https://production.example.com'

  const res = await fetch(url)
  return res.json()
}

export default async function Page() {
  const { title, desc } = await getData()

  return (
    <main>
      <h1>{title}</h1>
      <p>{desc}</p>
    </main>
  )
}

When the draft cookie is present, the page will render dynamically and show the latest draft content.


Conclusion


Draft Mode in Next.js provides a secure and efficient way to preview unpublished content from your CMS. By validating tokens and slugs, and dynamically rendering pages when needed, you can create a seamless editorial experience without compromising performance or security.


Written & researched by Dr. Shahin Siami

Related Articles

Advanced Client-Side Routing and Performance Hooks in Next.js

Next.js provides a rich set of client-side hooks and caching utilities that empower developers to build dynamic, responsive, and secure applications. From reading route parameters to tracking navigation state and reporting performance metrics, this guide walks you through the most important tools available in the App Router.

Continue

Handling Authorization and Caching in Next.js: A Developer’s Guide

Next.js introduces powerful experimental features for access control and smart caching. This guide covers the unauthorized() function for custom 401 handling, unstable_cache for persistent memoization, updateTag for instant cache invalidation, and useLinkStatus for inline navigation feedback. Learn how to use these tools to build secure, performant, and responsive applications.

Continue

redirect and refresh in Next.js — Smart Redirects and Client Refreshing via Server Actions

The redirect function in Next.js allows you to navigate users to a new route, returning either a 307 or 303 HTTP response depending on context. It works in Server Components, Client Components, Route Handlers, and Server Actions. The refresh function is used exclusively within Server Actions to refresh the client router. This article explains how both functions work, with practical examples and key considerations.

Continue

NextRequest and NextResponse in Next.js — Managing Cookies, Headers, Redirects, and Rewrites

Next.js extends the native Web Request and Response APIs with NextRequest and NextResponse, offering powerful tools for managing cookies, headers, redirects, rewrites, and JSON responses. These utilities simplify server-side logic and improve control over routing, personalization, and security. This guide walks through their capabilities with practical examples and best practices.

Continue

headers, ImageResponse, notFound, and permanentRedirect in Next.js — Request Handling, Dynamic Images, Errors, and Redirects

Next.js offers powerful tools for handling HTTP requests and responses in Server Components. The headers function lets you read incoming request headers. ImageResponse allows you to generate dynamic images using JSX and CSS. The notFound function renders a custom 404 page, and permanentRedirect enables permanent redirection to another route. This article explains how to use each feature with practical examples.

Continue

A Complete Guide to Using metadata and generateMetadata in Next.js

In modern versions of Next.js, managing page metadata is more powerful and intuitive than ever. Metadata is automatically injected into the <head> of your pages and plays a vital role in SEO, social sharing, and user experience. This guide explains the two main ways to define metadata: using the static metadata object and the dynamic generateMetadata function.

Continue