vercel / next.js

The React Framework
https://nextjs.org
MIT License
126.81k stars 26.95k forks source link

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

Open chandlervdw opened 11 months ago

chandlervdw commented 11 months ago

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

aleczratiu commented 11 months ago

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!

Jdruwe commented 8 months ago

@chandlervdw how did you manage to solve this?

chandlervdw commented 8 months ago

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

Jdruwe commented 8 months ago

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

alexclaes commented 2 weeks ago

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