Api have empty cookies for SSR request
See original GitHub issueBug report
Describe the bug
As sad in docs:
API routes provide built in middlewares which parse the incoming request. Those middlewares are: req.cookies - An object containing the cookies sent by the request.
But any API SSR request contain no cookies ({} object).
Also you can’t set cookies for SSR request from api.
Client-side requests at the same time have cookies and and be set from api.
To Reproduce
Run simple server
// pages/user
const User = () => {
return <div>test</div>
}
User.getInitialProps = async () => {
const init = await fetch('http://localhost/api/user')
return { init }
}
//pages/api/user
export default (req, res) => {
console.log(req.cookies) // will return {} on SSR
// will not set cookies on SSR
res.writeHead(200, {
'Set-Cookie': 'mycookie=test',
'Content-Type': 'text/plain'
})
.end(JSON.stringify('smth'))
}
Expected behavior
- req.cookies should contain cookies when page was SSR.
- You should be able to set cookies when page was SSR.
System information
- OS: [Windows]
- Browser [chrome 79.0.3945.88]
- Version of Next.js: [9.1.6]
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Why are cookies not sent to the server via getServerSideProps ...
That's because the request inside getServerSideProps doesn't run in the browser - where cookies are automatically sent on every request ...
Read more >Next.js — Handling Cookies in getServerSideProps
I want to fetch my userds data on the server before rendering the page, meaning Idll first need to validate the access token,...
Read more >Client and Server Side Cookies in Next.js - YouTube
Let's learn how to set/ remove cookies both in the browser but also on the server in Next.js. This will allow us to...
Read more >Quasar - SSR and using cookies - DEV Community
When building for SSR, use only the $q.cookies form. If you need to use the import { Cookies } from 'quasar', then you'll...
Read more >How to Use Cookies to Persist State in NextJS - Medium
make a request to sign in a user by calling a function to log the user in. We take the response from that...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

fetchdoes send cookies by default if the origin of the requests is the same since the default option forcredentialsissame-origin. It doesn’t work like that when running on the server because the server itself doesn’t store the cookies of previous requests, you need to do as Tim showed and reinclude the cookies from the request headers (because the browser stores the cookies, and will attach them automatically in the headers for each request to the server).@ivanhueso i use custom fetch like this: