Observe error Error: Invariant: Method expects to have requestAsyncStorage, none available
Describe the Bug
I want to enable Draft Mode from within Middleware. More specifically in the Middleware I am trying to:
Parse and validate some querystring values from the request
If the querystring values are valid, enable draft mode
I am aware from the examples in the Draft Mode docs that typically draft mode is enabled via API routes, but Middleware seemed like a better choice for me as I can validate the data already present in the querystring of the request rather than having to go to an API route first and then redirect to the actual location.
As Draft Mode is configured based on a cookie and you can set cookies from within Middleware I thought this would be possible, but the draftMode function from next/headers throws an error if you try to use it within Middleware, Error: Invariant: Method expects to have requestAsyncStorage, none available.
I noticed also that while the code examples in the Draft Mode documentation all use this draftMode function, there are a lot of examples (e.g. cms-storyblok that show another function available on NextApiResponse called setDraftMode, but that is not available within Middleware.
It seems like setting the Draft Mode cookie from within Middleware should be possible, but I cannot find any way to do so.
Expected Behavior
I would like some way of setting the Draft Mode cookie from within Middleware so that Draft Mode can be enabled and used when fetching data from a page/layout/component within a single request.
Which browser are you using? (if relevant)
Chrome 114.0.5735.198
How are you deploying your application? (if relevant)
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.
Verify canary release
Provide environment information
Which area(s) of Next.js are affected? (leave empty if unsure)
Middleware / Edge (API routes, runtime)
Link to the code that reproduces this issue or a replay of the bug
https://github.com/CMeeg/nextjs-middleware-draftmode
To Reproduce
Using the minimal repro:
next dev
http://localhost:3000/
in a browserError: Invariant: Method expects to have requestAsyncStorage, none available
Describe the Bug
I want to enable Draft Mode from within Middleware. More specifically in the Middleware I am trying to:
I am aware from the examples in the Draft Mode docs that typically draft mode is enabled via API routes, but Middleware seemed like a better choice for me as I can validate the data already present in the querystring of the request rather than having to go to an API route first and then redirect to the actual location.
As Draft Mode is configured based on a cookie and you can set cookies from within Middleware I thought this would be possible, but the
draftMode
function fromnext/headers
throws an error if you try to use it within Middleware,Error: Invariant: Method expects to have requestAsyncStorage, none available
.I noticed also that while the code examples in the Draft Mode documentation all use this
draftMode
function, there are a lot of examples (e.g. cms-storyblok that show another function available onNextApiResponse
calledsetDraftMode
, but that is not available within Middleware.It seems like setting the Draft Mode cookie from within Middleware should be possible, but I cannot find any way to do so.
Expected Behavior
I would like some way of setting the Draft Mode cookie from within Middleware so that Draft Mode can be enabled and used when fetching data from a page/layout/component within a single request.
Which browser are you using? (if relevant)
Chrome 114.0.5735.198
How are you deploying your application? (if relevant)
No response