Error: Invariant: cookies() expects to have requestAsyncStorage, none available.
Answered
Balinese posted this in #help-forum
BalineseOP
This error seems to be occuring when called inside generateStaticParams(). I had used the same function in a normal page component, but this particular dynamic route page seems to have a problem.
Answered by aardani
export async function isAllowed(slug: string){
try {
await getAuthData(slug) // <-- also has cookies() in there to check against user cookies which are going to be sent to the db/backend
} catch(error) {
notFound()
}
}24 Replies
@Balinese This error seems to be occuring when called inside generateStaticParams(). I had used the same function in a normal page component, but this particular dynamic route page seems to have a problem.
the
generateStaticParams function runs when your app is built and without any request, so there will be no cookies@@ts-ignore the `generateStaticParams` function runs when your app is built and without any request, so there will be no cookies
BalineseOP
Does that mean this has be a client component instead? What is the best workaround for this you think?
@Balinese Does that mean this has be a client component instead? What is the best workaround for this you think?
you cant use generateStaticParams if you want to read user's cookie
Californian
no import?
which meant opting into request-time dynamic SSR
BalineseOP
Context:
I want to make an API request to fetch a list of data from my backend, that data is supposed to passed into generateStaticParams (dynamic segments). This request can only be made with an auth token (stored in cookies).
I want to make an API request to fetch a list of data from my backend, that data is supposed to passed into generateStaticParams (dynamic segments). This request can only be made with an auth token (stored in cookies).
Is using generateStaticParams in this context the correct way?
no
because with generateStaticParams you are generating page at build time. In build time, no user means no request, no request means no cookie
what are you trying to achieve?
BalineseOP
I'm trying to create dynamic routes with authenticated users. Each user has their own unique data (which also means the slug in the route will always be different for each user)
ahh which mean you can't possibly check that at build time right? then you can't use
generateStaticParamsBalineseOP
Hmm yeah
you need to check the "unique data" in the [id] slug and see if they had access to
idif not then send them to notFound()
this is the SSR way, processing data at request-time
BalineseOP
So I just have to not use generateStaticParams?
yeah
export async function isAllowed(slug: string){
try {
await getAuthData(slug) // <-- also has cookies() in there to check against user cookies which are going to be sent to the db/backend
} catch(error) {
notFound()
}
}Answer
BalineseOP
I just have to do this?
yeah
BalineseOP
Ahh ok
if not allowed then call notFound() from next/navigation
BalineseOP
Thanks so much! I know what to do now 🙂