Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Important
Security Advisory: React2Shell & two new vulnerabilities
Find out more

draftMode

Last updated June 16, 2025

draftMode is anasync function allows you to enable and disableDraft Mode, as well as check if Draft Mode is enabled in aServer Component.

app/page.ts
import { draftMode }from'next/headers'exportdefaultasyncfunctionPage() {const {isEnabled }=awaitdraftMode()}

Reference

The following methods and properties are available:

MethodDescription
isEnabledA boolean value that indicates if Draft Mode is enabled.
enable()Enables Draft Mode in a Route Handler by setting a cookie (__prerender_bypass).
disable()Disables Draft Mode in a Route Handler by deleting a cookie.

Good to know

  • draftMode is anasynchronous function that returns a promise. You must useasync/await or React'suse function.
    • In version 14 and earlier,draftMode was a synchronous function. To help with backwards compatibility, you can still access it synchronously in Next.js 15, but this behavior will be deprecated in the future.
  • A new bypass cookie value will be generated each time you runnext build. This ensures that the bypass cookie can’t be guessed.
  • To test Draft Mode locally over HTTP, your browser will need to allow third-party cookies and local storage access.

Examples

Enabling Draft Mode

To enable Draft Mode, create a newRoute Handler and call theenable() method:

app/draft/route.ts
import {draftMode }from'next/headers'exportasyncfunctionGET(request:Request) {constdraft=awaitdraftMode()draft.enable()returnnewResponse('Draft mode is enabled')}

Disabling Draft Mode

By default, the Draft Mode session ends when the browser is closed.

To disable Draft Mode manually, call thedisable() method in yourRoute Handler:

app/draft/route.ts
import {draftMode }from'next/headers'exportasyncfunctionGET(request:Request) {constdraft=awaitdraftMode()draft.disable()returnnewResponse('Draft mode is disabled')}

Then, send a request to invoke the Route Handler. If calling the route using the<Link> component, you must passprefetch={false} to prevent accidentally deleting the cookie on prefetch.

Checking if Draft Mode is enabled

You can check if Draft Mode is enabled in a Server Component with theisEnabled property:

app/page.ts
import { draftMode }from'next/headers'exportdefaultasyncfunctionPage() {const {isEnabled }=awaitdraftMode()return (    <main>      <h1>My Blog Post</h1>      <p>Draft Mode is currently {isEnabled?'Enabled':'Disabled'}</p>    </main>  )}

Version History

VersionChanges
v15.0.0-RCdraftMode is now an async function. Acodemod is available.
v13.4.0draftMode introduced.

Next Steps

Learn how to use Draft Mode with this step-by-step guide.

Was this helpful?

supported.

[8]ページ先頭

©2009-2025 Movatter.jp