set cookies in ssr in next js 14 after getting response from api #62323
Replies: 1 comment 5 replies
-
You can set cookies in server-side rendering (SSR) after getting a response from an API by using the setHeader method in the response object (res). Example:
In this example, after fetching data from an API, a cookie is set in the response headers before sending the data back to the client. The Set-Cookie header is used to set the cookie, where cookieName is the name of your cookie, and cookieValue is its value. You can also add other cookie attributes as needed, such as Expires, Max-Age, Domain, Path, Secure, and HttpOnly. If you're setting cookies in a page's getServerSideProps function, you can use the res object in a similar way:
In this case, the cookie is set in the response before the page is rendered and sent to the client. |
Beta Was this translation helpful? Give feedback.
-
can any one have idea or code for set cookies in ssr in next js 14 after getting response from api
Beta Was this translation helpful? Give feedback.
All reactions