Based on the knowledge sources provided, I can address your question about handling secure cookies in Next.js with a NestJS backend, specifically focusing on the difference between client-side and server-side requests.
When making requests from server-side components in Next.js to your NestJS backend, the cookies are not automatically included in the request. This is because server-side components run on the Next.js server, which doesn't have direct access to the browser's cookies.
As mentioned in the Github issue comment (https://github.com/vercel/next.js/issues/52209#issuecomment-1621889571):
"Basically, it depends on where you are calling the API endpoint from; keep note that the server doesn't have direct access to the cookies because they are stored on the browser and are sent with a request made to an API endpoint from the client. Therefore, if you are calling the API endpoint from the client (i.e. Browser), the cookies are sent alongside the request. But, if you are calling the API endpoint from a server component or another API endpoint in your application, the cookies are not sent alongside the request, because the request is being sent from the SERVER and NOT the CLIENT!"
This explains why you're seeing the cookies included when making requests from the client-side components (those with the 'use client' directive), but not from server-side components.
To handle this situation, you have a few options:
- Pass cookies manually: You can pass the cookies from the client to the server-side component, and then include them in your request to the NestJS backend. The Next.js documentation on Accessing Request Object (https://nextjs.org/docs/app/building-your-application/upgrading/app-router-migration#accessing-request-object) shows how to access cookies in server components: