Next.js Discord

Discord Forum

How to get cookie immediately on server side

Unanswered
Black Caiman posted this in #help-forum
Open in Discord
Black CaimanOP
In the middleware, I used the NextResponse.next().cookies.set function to set a cookie, and the cookie has been successfully written, as I can see it in the browser.

However, during the initial page rendering, I cannot retrieve this cookie using the cookies().get method on the server-side.

How can I obtain this cookie during the initial rendering?

4 Replies

@joulev try tsx export function middleware(request: NextRequest) { request.cookies.set(...) }
Black CaimanOP
thx, but I have tried it, and it didn't work. I still couldn't retrieve the cookie.
French Angora
maybe try this
request.cookies.get(...).value
@French Angora maybe try this js request.cookies.get(...).value
Black CaimanOP
This function is used to request the cookie, but in reality, I need to retrieve this cookie in a certain utility function, and I need to use it both on the client-side and the server-side.