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 not getting the updated cookie from the middleware

See original GitHub issue

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Operating System: Platform: darwin Arch: x64 Version: Darwin Kernel Version 21.1.0 Binaries: Node: 14.17.1 npm: N/A Yarn: 1.22.17 pnpm: N/A Relevant packages: next: 12.1.5-canary.4 react: 17.0.2 react-dom: 17.0.2

What browser are you using? (if relevant)

Chrome Version 99.0.4844.84 (Official Build) (arm64)

How are you deploying your application? (if relevant)

next start

Describe the Bug

In the Middleware, when I set the cookie to some new value or a new cookie and I need to use it in the getServerSideProps, I cant able to get those cookies.

Expected Behavior

I can able to see the cookie in the browser, but the new/updated cookie should be present in getServerSideProps, aswell.

To Reproduce

In Middleware.ts const pages = ['/home/']; async function middleware(request: NextRequest) { if (pages.includes(request.nextUrl.pathname)) { return nextResponse.cookie('newCookie','some value') } } in [page.tsx] export const getServerSideProps: GetServerSideProps = async (context) => { console.log(context?.req?.headers); return {props:{}} } I cant able to see the newCookie in the [page.tsx] console statement.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:5
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
iljamulderscommented, Jul 20, 2022

https://github.com/vercel/next.js/discussions/38650#discussioncomment-3148772

The request is immutable, so it arrives to the Node.js server without changes, that’s why your cookie is missing.

What you can do is a bit of a magic trick… because you can read the response in getServerSideProps, and from there getHeader, called set-cookie.

export function getServerSideProps({ req, res }) {
  console.log('Cookie from accessToken:', context.res.getHeader('set-cookie'));
  return { props: {} };
}

And from there you just have to parse that cookie, and possibly remove it or let it through.

1reaction
thebearingedgecommented, Jul 21, 2022

@iljamulders galaxy brain 👏

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cookies Set In Middleware Not Available on ... - GitHub
After few debugging I found out that "async" function on middleware and getserversideprops affect serverside runtime. So while middleware still ...
Read more >
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 getServerSideProps() doesn't have access to cookies ...
in my index.js file I'm trying to get the cookie before the page is rendered: export async function getServerSideProps(context) { let ...
Read more >
Data Fetching: getServerSideProps - Next.js
This is useful if you want to fetch data that changes often, and have the page update to show the most current data....
Read more >
Data Fetching: getServerSideProps - Next.js
This could be due to the nature of the data or properties of the request (such as authorization headers or geo location). Pages...
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