Next.js Discord

Discord Forum

Unexpected middleware/cookies() behavior

Unanswered
Polish posted this in #help-forum
Open in Discord
PolishOP
When a cookie is set in the middleware but blocked by the browser, if you use cookies() in the layout or page it will still retrieve the cookies that were never actually set. cookies() appears to be accessing both the cookies from the response and request vs just the request. I don't see a workaround.

1 Reply

PolishOP
Even more weird. This issue only occurs when the cookie is set in the NextResponse.next()

const response = NextResponse.next();
response.cookies.set({*variables*});
return response;


Setting it with cookies() seems to work as expected. If the cookie is blocked by the browser, other cookies() can't access it
const cookieStore = await cookies();
cookieStore.set({*variables*});
return NextResponse.next();


This is in the middleware