# Draft Mode

> Preview CMS changes without breaking cache.

Subject: Next.js | Lesson 62 of 65 | 3 min read
Interactive lesson: https://codewithanimation.in/nextjs/draft-mode
Previous: [Edge vs Node.js Runtime](https://codewithanimation.in/nextjs/edge-vs-node-runtime.md) | Next: [React Compiler Integration (v15)](https://codewithanimation.in/nextjs/react-compiler.md)

## Bypassing the Cache Securely

Next.js heavily caches pages for performance (SSG). But content editors need a way to preview unpublished drafts from a Headless CMS (like Sanity or Contentful) *before* publishing.

**Draft Mode** solves this by setting a secure, signed cookie on the editor's browser. When Next.js detects this cookie, it temporarily bypasses the Full Route Cache and Data Cache, rendering the page dynamically.

## Performance Isolation

You don't want to disable caching for everyone just because one editor is previewing a post.

Draft Mode ensures that the 99.9% of regular visitors continue to get the blazing fast, cached, statically generated version of the site, while the 0.1% (the editors) get dynamic, real-time draft data.

## Enabling Draft Mode

First, create a Route Handler to enable it. This should be secured with a secret token.

```typescript
// app/api/draft/route.ts
import { draftMode } from 'next/headers';
import { redirect } from 'next/navigation';

export async function GET(request: Request) {
  const { searchParams } = new URL(request.url);
  if (searchParams.get('secret') !== process.env.MY_SECRET) {
    return new Response('Invalid token', { status: 401 });
  }
  
  draftMode().enable(); // Sets the signed cookie
  redirect('/post/my-new-post');
}
```

Then, in your Server Component, check if it's enabled:

```typescript
// app/post/[slug]/page.tsx
import { draftMode } from 'next/headers';

export default async function Page({ params }) {
  const { isEnabled } = draftMode();
  // Fetch draft data if enabled, otherwise fetch live cached data
  const post = await fetchCMS(params.slug, { draft: isEnabled });
  return <article>{post.title}</article>;
}
```

## Common mistake: The Unsecured Route Trap

If you forget to require a `?secret=...` token in your Draft Mode Route Handler, anyone can hit `/api/draft`.

If a malicious bot discovers this, they can enable Draft Mode for themselves. Because Draft Mode bypasses all caching, they can easily DDoS your underlying CMS database with thousands of un-cached requests.

## Try it: Cache Isolation Simulator

Watch how the Edge Network routes requests differently based on the presence of the Draft Mode cookie.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/draft-mode

## Check yourself

### Question 1

How does Draft Mode affect caching for regular visitors on your site?

- A. It disables caching globally until the draft is published.
- B. It clears the cache so the next visitor generates the new page.
- C. It sets a cookie in the editor's browser, bypassing the cache ONLY for them.
- D. It forces Client Components to fetch data instead of Server Components.

Correct answer: C

Draft Mode is isolated to the specific browser that has the signed Draft Mode cookie. Regular visitors without the cookie continue to hit the fast, static Full Route Cache.

- Why not B: That is On-Demand Revalidation, not Draft Mode.

## Remember this

- Draft Mode allows previewing headless CMS drafts without sacrificing SSG performance.
- It uses a signed cookie to bypass the Full Route Cache and Data Cache.
- Always secure your `/api/draft` route with a secret token to prevent cache-bypassing attacks.
- Access it via `import { draftMode } from 'next/headers';` in Server Components.
