/pages/post/[id].tsx
export async function getServerSideProps(context: GetServerSidePropsContext) {
const { id } = context.params as Params
const { req, res } = context
//this cookie is undefined in vercel deployment
const { cookie } = req?.headers
const parsedCookie = cookie && parse(cookie as string)
res.setHeader('Cache-Control', 's-maxage=20, stale-while-revalidate')
const postDetails = await getPostDetails(id as string)
if (parsedCookie) {
return {
props: {
postDetail: postDetails,
cookie: parsedCookie
}
}
} else {
//this statement runs in deployed site because of no cookies
return {
props: {
postDetail: postDetails,
cookie: {
accessToken: '',
refreshToken: ''
}
}
}
}
}
This is how I am setting http only cookie from my separate express api.
//server/auth.ts
const environment = process.env.NODE_ENV // it is "production" in .env file of production
res.cookie('refreshToken', refreshToken, {
httpOnly: true,
secure: environment === 'production',
sameSite: environment === 'production' ? 'none' : 'lax',
path: '/'
})
res.cookie('accessToken', accessToken, {
httpOnly: true,
secure: environment === 'production',
sameSite: environment === 'production' ? 'none' : 'lax',
path: '/'
})
I am using NextJs pages and separate express backend. In local development, the cookies are available inside ‘getServerSideProps” and can access it. And I can see accessToken from it to my backend through axios.
But the deployed site on vercel has empty or undefined cookie “req.headers.cookie” while trying to access inside getServerSideProps in production. But yeah, with client side fetching even in vercel deployed site, I can see access token being available to backend. So, no problem of cookie not being set on deployed frontend domain. Just that there is cookies are undefined only inside getServerSideProps in vercel deployment.
What might be the issue? Am I missing something? Has anyone encountered this issue? I tried all methods of setting path to “/” and all others but still no luck
In local development, cookies are being populated in getServerSideProps as seen in the picture
Empty or undefined cookie just in vercel deployed domain(production) that’s why second else statement ran