question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`getServerSideProps` does not set cookies using client side navigation

See original GitHub issue

Bug 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.

  1. 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' ]
}
  1. Using router.push('/logout') or <Link href="/logout"><a>Logout</a></Link> prints the exact same message on the server log and renders the Logout page, but ignores the Set-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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dalfriedricecommented, Nov 3, 2020

I am facing some what same issue I stored a cookie using document.cookie on my page route /contentfulAuth and then used Router.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 as path="/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 to path="/landingpages", it don’t finds it

But the above scenario works well if I use window.location

0reactions
github-actions[bot]commented, Nov 30, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why are cookies not sent to the server via getServerSideProps ...
This means you need to explicitly pass the cookies to the axios request to send them through. export async function getServerSideProps({ req }) ......
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 >
Best practices to increase the speed for Next.js apps
Using server-side rendering will help your app reduce the time required to render the first page on the client side, so the user...
Read more >
Advanced Features: Internationalized Routing - Next.js
The default locale does not have a prefix. Domain Routing. By using domain routing you can configure locales to be served from different ......
Read more >
next.config.js: Redirects
Redirects are not applied to client-side routing ( Link , router.push ), unless Middleware is present and matches the path. When a redirect...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found