How does serverAction see modified cookie that middleware sets through response.cookies.set
Unanswered
American Wirehair posted this in #help-forum
American WirehairOP
I am a bit confused with how request => middleware => set response cookie in middleware => server action reads cookie interaction works.
All of the AI agents are keep saying that if I make a server action request and set a cookie in middleware, the server action execution will not see the newly set cookie value intil the response reaches the browser but all my tests prove the opposite.
How does this mechanism actually work and why is there no documentation for this behaviour? From what I understand so far, if I set a cookie value on respose object in middleware, when the middleware returns and server action runs, doing cookie.get will actually have the cookie that we just set in middleware. From brief look at the source code I believe this happens because of mergeMiddlewareCookies in request-async-storage-wrapper.js but I am not 100% convinced yet and a bit confused as to why there is no documentation description for this.
Does anyone know how this works?
All of the AI agents are keep saying that if I make a server action request and set a cookie in middleware, the server action execution will not see the newly set cookie value intil the response reaches the browser but all my tests prove the opposite.
How does this mechanism actually work and why is there no documentation for this behaviour? From what I understand so far, if I set a cookie value on respose object in middleware, when the middleware returns and server action runs, doing cookie.get will actually have the cookie that we just set in middleware. From brief look at the source code I believe this happens because of mergeMiddlewareCookies in request-async-storage-wrapper.js but I am not 100% convinced yet and a bit confused as to why there is no documentation description for this.
Does anyone know how this works?
1 Reply
1. Stop asking AI agents. Nextjs change rapidly and AI agents can't keep up
2. The phrasing "middleware" is incorrect so the team has renamed it to "proxy.ts" in latest release to make it more clear
3. The middleware still set the "set-cookie" header which I think the server actions(the await cookies()) call consider and add to the object.
the 3rd part is what I think it is and I am not really sure
2. The phrasing "middleware" is incorrect so the team has renamed it to "proxy.ts" in latest release to make it more clear
3. The middleware still set the "set-cookie" header which I think the server actions(the await cookies()) call consider and add to the object.
the 3rd part is what I think it is and I am not really sure