mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
## What Using methods from `next/headers` in middleware would throw a `requestAsyncStorage` invariant. Additionally, using `draftMode()` in middleware/an edge runtime is not possible ## Why We do not expose `requestAsyncStorage` to the middleware adapter. Also, the prerender manifest wasn't available to the `EdgeRouteModuleWrapper` & middleware adapter, so it wasn't possible to enable/disable it. ## How This makes `requestAsyncStorage` available for middleware, and makes the preview mode data available from build to the edge runtime/middleware. Fixes #52557
26 lines
552 B
TypeScript
26 lines
552 B
TypeScript
import React from 'react'
|
|
import { cookies, draftMode } from 'next/headers'
|
|
|
|
export default function Page() {
|
|
const { isEnabled } = draftMode()
|
|
let data: string | undefined
|
|
if (isEnabled) {
|
|
data = cookies().get('data')?.value
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<h1>Draft Mode with dynamic cookie</h1>
|
|
<p>
|
|
Random: <em id="rand">{Math.random()}</em>
|
|
</p>
|
|
<p>
|
|
State: <strong id="mode">{isEnabled ? 'ENABLED' : 'DISABLED'}</strong>
|
|
</p>
|
|
<p>
|
|
Data: <em id="data">{data}</em>
|
|
</p>
|
|
</>
|
|
)
|
|
}
|