vercel / next.js

The React Framework
https://nextjs.org
MIT License
126.92k stars 26.98k forks source link

middleware request.cookies.has() fails when route is hit via Link #69058

Open janson opened 2 months ago

janson commented 2 months ago

Link to the code that reproduces this issue

https://github.com/janson/nextjs-cookies-middlware/tree/main

To Reproduce

  1. deploy project to Vercel
  2. visit /
  3. visit /demo

Current vs. Expected behavior

Expected

A single console log message, ***** Setting cookie: experiments *****' is displayed when the cookie is set upon first visiting /demo. No repeated message until the cookie is deleted or expired.

Current Cookie is set several times upon first visiting the / page. And again when visiting /demo.

Screenshot 2024-08-19 at 9 23 47 AM

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:46 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6031
  Available memory (MB): 65536
  Available CPU cores: 16
Binaries:
  Node: 20.12.0
  npm: 10.5.0
  Yarn: 1.22.21
  pnpm: 8.6.11
Relevant Packages:
  next: 14.2.5 // Latest available version is detected (14.2.5).
  eslint-config-next: 14.2.5
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.5.4
Next.js Config:
  output: N/A
✨  Done in 1.68s.

Which area(s) are affected? (Select all that apply)

Middleware

Which stage(s) are affected? (Select all that apply)

Vercel (Deployed), Other (Deployed)

Additional context

eps1lon commented 2 months ago

Thank you for providing the repro for the Vercel deploy

Bug also observed with the official doc's cookie+middleware example

Can you attach a repository reproducing that behavior?

janson commented 2 months ago

Hi @eps1lon sure thing:

https://github.com/janson/nextjs-cookies-middleware-vercel

Just visiting the homepage in the browser sets the 'vercel' cookie three times.

Screenshot 2024-08-19 at 10 08 25 AM