Skip to content

Draft Mode does not work in Middleware #67075

@JKarlavige

Description

@JKarlavige

Link to the code that reproduces this issue

https://github.com/JKarlavige/nextjs-draftmode-reproduction

To Reproduce

  1. Clone reproduction repository locally
  2. Install packages with npm i
  3. Run app with npm run dev
  4. In a browser, hit the following url: localhost:3000/api/draft?secret=secret-token&slug=preview-page

Current vs. Expected behavior

Note - Draft mode on the page itself works correctly. draftMode().isEnabled returns true.

Current behavior:
In middleware, draftMode().isEnabled returns false after hitting the draft endpoint in step 4 above.

Expected behavior:
draftMode().isEnabled should return true after the draft endpoint has been hit.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.5.0: Wed May  1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 10
Binaries:
  Node: 20.11.1
  npm: 10.2.4
Relevant Packages:
  next: 14.2.4 // Latest available version is detected (14.2.4).
  react: 18.3.1
  react-dom: 18.3.1

Which area(s) are affected? (Select all that apply)

Middleware

Which stage(s) are affected? (Select all that apply)

next dev (local), Vercel (Deployed)

Additional context

On next version 14.1.4 (which we currently use in production), we experience inconsistencies where draft mode in middleware sometimes returns true, and on the following middleware run it returns false.

On version 14.2.4, it appears that draft mode always returns false.
middleware-draftmode

Metadata

Metadata

Assignees

No one assigned

    Labels

    MiddlewareRelated to Next.js Middleware.bugIssue was opened via the bug report template.locked

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions