next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Docs: Improve error messaging around `draftMode()` when used in `generateStaticParams`

Open chandlervdw opened this issue 2 years ago • 5 comments

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

chandlervdw avatar Nov 21 '23 15:11 chandlervdw

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!

aleczratiu avatar Dec 10 '23 18:12 aleczratiu

@chandlervdw how did you manage to solve this?

Jdruwe avatar Feb 29 '24 13:02 Jdruwe

@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 avatar Mar 04 '24 19:03 chandlervdw

@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()

Yea, thanks I did the same in the end :)

Jdruwe avatar Mar 04 '24 19:03 Jdruwe

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

alexclaes avatar Oct 24 '24 09:10 alexclaes

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 🤷

markomitranic avatar Dec 05 '24 10:12 markomitranic