next.js
next.js copied to clipboard
Docs: Improve error messaging around `draftMode()` when used in `generateStaticParams`
What is the improvement or update you wish to see?
Currently, when draftMode() is utilized directly or in a function reference within generateStaticParams, the cryptic error message is as follow:
Error: Invariant: draftMode() expects to have requestAsyncStorage, none available.
It took me forever to figure out that it it was coming from calling a fetch() function that uses draftMode() from within generateStaticParams. Either the messaging needs to be improved, or draftMode().isEnabled should be stubbed to false within that context.
Is there any context that might help us understand?
It would also be helpful to explain why draftMode() hasn't initialized when pre-rendering.
Does the docs page already exist? Please link to it.
https://nextjs.org/docs/app/building-your-application/configuring/draft-mode
Any updates here?
Edit by maintainers: Comment was automatically minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!
@chandlervdw how did you manage to solve this?
@chandlervdw how did you manage to solve this?
@Jdruwe I made a new fetch function that was specifically for generateStaticParams that didn’t reference draftMode()
@chandlervdw how did you manage to solve this?
@Jdruwe I made a new fetch function that was specifically for
generateStaticParamsthat didn’t referencedraftMode()
Yea, thanks I did the same in the end :)
Just encountered the same problem in Next.js 14.2, but the error message seems to be changed now:
Error: `draftMode` was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context
This message is more helpful. However, it took me quite a while to realize it had to do with something happening in generateStaticParams
I've had a very deep dependency on draftMode, where In my adapter layer, I check if we are in draft mode, in order to decide to call a CDN or the DB directly. In any case, it was kinda hard to refactor all of that, so i simply wrapped the const draft = (await draftMode()).isEnabled; invocation into a try catch block:
const draft = await (async () => {
try {
return (await draftMode()).isEnabled;
} catch {
return false;
}
})();
Might not be ideal, but helped me upgrade to next15 🤷