Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookies missing in Nextjs with app directory if set cookie in middleware #50726

Closed
1 task done
iMuFeng opened this issue Jun 3, 2023 · 5 comments
Closed
1 task done
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template. locked

Comments

@iMuFeng
Copy link

iMuFeng commented Jun 3, 2023

Verify canary release

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

Provide environment information

Relevant packages:
      next: 13.4.4
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true)

Link to the code that reproduces this issue or a replay of the bug

https://github.com/iMuFeng/nextjs-app-cookie

To Reproduce

Run git clone https://github.com/iMuFeng/nextjs-app-cookie
Run cd nextjs-app-cookie && pnpm i && pnpm dev
Open http://127.0.0.1:3000/
Create a new cookie named "cookie1" manually in the console with any value (like abc), and then refresh this page.

Describe the Bug

Nextjs has a bug where the cookies from the response are parsed by nextjs/headers cookies instead of those from the request.

TinySnap-2023-06-03-16 31 02
TinySnap-2023-06-03-16 31 11

Expected Behavior

There should display the value of "cookie1"

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@iMuFeng iMuFeng added the bug Issue was opened via the bug report template. label Jun 3, 2023
@github-actions github-actions bot added the area: app App directory (appDir: true) label Jun 3, 2023
@iMuFeng iMuFeng changed the title Cookies missing in Nextjs with app directory if set or refresh session in middleware Cookies missing in Nextjs with app directory if set cookie in middleware Jun 3, 2023
@pnodet
Copy link

pnodet commented Jun 19, 2023

@iMuFeng having the same issue, inconsistent behaviour where cookies().getAll() returns [] but cookies are correctly appearing in the chrome dev application tab.

What did you end up doing?

@iMuFeng
Copy link
Author

iMuFeng commented Jun 19, 2023

@pnxdxt The issue I encountered was that Supabase added the cookie field in the HTTP response header. After removing it, I was able to retrieve other cookies.

@pnodet
Copy link

pnodet commented Jun 19, 2023

@iMuFeng Thanks! For anyone looking this up, we had this line

export const dynamic = ‘force-static’

which didn’t seem to work well with server-side cookies 😌

@shuding
Copy link
Member

shuding commented Jun 19, 2023

You should forward the headers from the request to the middleware response, like this:

export default async function (req) {
  const res = NextResponse.next({
    headers: req.headers,
  })

  // add cookies to `res.headers` ...

  return res
}

How Next.js middleware currently works is, if the middleware doesn't handle the request at all (e.g. it just return nothing):

CleanShot-2023-06-19-L9jEHwxF@2x

The user request will be directly forwarded to the page because the middleware skips handling.

However, if the middleware accepts that request and returns a new response (e.g. return NextResponse.next()), it will be this:

CleanShot-2023-06-19-wEDpoToG@2x

Since you want the page to accept headers from the original user request, you need to pass them as init to the middleware's response constructor.

Let me know if you have other questions!

@shuding shuding closed this as completed Jun 19, 2023
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template. locked
Projects
None yet
Development

No branches or pull requests

3 participants