`getServerSideProps` does not set cookies using client side navigation
See original GitHub issueBug report
Describe the bug
I apologize up front if this was covered somewhere else - couldn’t find a related source for this. I found out that using router.push
(with default options, i.e.: { shallow: false }
) or <Link>
effectively calls getServerSideProps
but ignores response cookies set from them. On the flip side, using window.location
or simply pointing the browser to the path, works as expected.
To Reproduce
I happen to have a /logout
page exporting the following:
export function getServerSideProps(ctx) {
// Delete token cookie...
removeAuthToken(ctx);
console.log('removed auth token', ctx.res.getHeaders());
// ...and take users back to login screen
return { props: { isAuthenticated: false, redirectTo: '/login' } };
}
removeAuthToken
does what you’d expect it to do: it sets a new Set-Cookie
header clearing the HTTP only session cookie.
- Using
window.location = '/logout';
, regular<a>
tags or manually navigating to/logout
, effectively removes the corresponding cookie clients side. On the server console, it reads:
removed auth token [Object: null prototype] {
'set-cookie': [ '__token=; Max-Age=-1' ]
}
- Using
router.push('/logout')
or<Link href="/logout"><a>Logout</a></Link>
prints the exact same message on the server log and renders theLogout
page, but ignores theSet-Cookie
header, keeping the user logged in.
I’ve resorted to window.location
for these cases, for the time being - but what am I doing wrong here?
Expected behavior
next/router
and next/link
, when not using shallow
navigation, should respect Set-Cookie
headers set from getServerSideProps
.
System information
- OS: macOS (Catalina
10.15.5
) - Browser Chrome
83.0.4103.116
- Version of Next.js:
9.4.4
- Version of Node.js:
v12.18.2
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:11 (3 by maintainers)
Top GitHub Comments
I am facing some what same issue I stored a cookie using
document.cookie
on my page route/contentfulAuth
and then usedRouter.push(/landingpages)
Now when it comes to/landingpages
, getServerSideProps is not able to read my cookie set in/contentfulAuth
.Things that I already took care of, shallow routing is working, as getServerSideProps gets called in
landingpages
, I have set the cookie path aspath="/landingpages"
, but there is an interesting case here When i set path as the root path (ie path="/"
), getServerSideProps able to access it, but if I change the path of cookie topath="/landingpages"
, it don’t finds itBut the above scenario works well if I use
window.location
We cannot recreate the issue with the provided information. Please add a reproduction in order for us to be able to investigate.
Why was this issue marked with the
please add a complete reproduction
label?To be able to investigate, we need access to a reproduction to identify what triggered the issue. We prefer a link to a public GitHub repository (template), but you can also use a tool like CodeSandbox or StackBlitz.
To make sure the issue is resolved as quickly as possible, please make sure that the reproduction is as minimal as possible. This means that you should remove unnecessary code, files, and dependencies that do not contribute to the issue.
Please test your reproduction against the latest version of Next.js (
next@canary
) to make sure your issue has not already been fixed.I added a link, why was it still marked?
Ensure the link is pointing to a codebase that is accessible (e.g. not a private repository). “example.com”, “n/a”, “will add later”, etc. are not acceptable links – we need to see a public codebase. See the above section for accepted links.
What happens if I don’t provide a sufficient minimal reproduction?
Issues with the
please add a complete reproduction
label that receives no meaningful activity (e.g. new comments with a reproduction link) are automatically closed and locked after 30 days.If your issue has not been resolved in that time and it has been closed/locked, please open a new issue with the required reproduction.
I did not open this issue, but it is relevant to me, what can I do to help?
Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps. Furthermore, you can upvote the issue using the 👍 reaction on the topmost comment (please do not comment “I have the same issue” without repro steps). Then, we can sort issues by votes to prioritize.
I think my reproduction is good enough, why aren’t you looking into it quicker?
We look into every Next.js issue and constantly monitor open issues for new comments.
However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.
Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.
Useful Resources