astro icon indicating copy to clipboard operation
astro copied to clipboard

🐛 BUG: `Astro.request.headers.get('cookie')` returns `null` on Node v18

Open JanotLeLapin opened this issue 2 years ago • 1 comments

What version of astro are you using?

1.0.0-beta.36

Are you using an SSR adapter? If so, which one?

@astrojs/node v0.1.2

What package manager are you using?

npm

What operating system are you using?

Linux

Describe the Bug

I tested Astro.request.headers.get('cookie') with Node v17.9.0 and v18.2.0. The function returned an expected result with v17.9.0, and always returned null with v18.2.0. I can see from the browser that the cookies are sent to the header for each request regardless of the Node version.

This bug might be related to the Fetch API updates regarding Node v18 ?

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-zpsns9?file=src/pages/index.astro

Participation

  • [ ] I am willing to submit a pull request for this issue.

JanotLeLapin avatar May 28 '22 19:05 JanotLeLapin

I did a little bit of digging myself here, to try to figure out the root cause of this issue.

I found that Node v18 uses HeadersList (as a class), while v17 does not. You can see the headers code from Undici here: https://github.com/nodejs/undici/blob/main/lib/fetch/headers.js

Logging this in the core request.js file: console.log("headers", headers instanceof Headers, headers, new Headers(Object.entries(headers)))

I get this (shortened for brevity):

v17

headers false {
....
} {
....
} object

v18

headers false {
...
} HeadersList {
  [Symbol(headers map)]: Map(11) {
...
  },
  [Symbol(headers map sorted)]: null
} object

I also noticed that if I logged a header before the initial Request class is created, I can easily get the header's value.

However, getting the data (on v18) from the created Request, returns null.

This could be an issue with Node.js v18 or Undici, but a fix may be able to be implemented in Astro.

jackmerrill avatar Jun 15 '22 22:06 jackmerrill

Hi, I forked the previous stackblizt and updated both astro to 1.0.6 and the config file to output to 'server' [0]. The problem persists. I think this is quite a relevant issue because it invalidates the whole SSR feature.

[0] https://stackblitz.com/edit/github-zpsns9-pvoi73?file=astro.config.mjs

ricogallo avatar Aug 17 '22 14:08 ricogallo

I'm also seeing the same behavior when attempting to read the cookie from an APIRoute. This is breaking the SSR example.

I'd recommend bumping this up to a p1 since it makes SSR apps that require authentication completely broken.

coryvirok avatar Sep 10 '22 06:09 coryvirok

Upgrading to Node 18.9 (from 18.2) seems to fix it.

Also, more info here: https://discord.com/channels/830184174198718474/1004159859131944981/1004464449601667154

coryvirok avatar Sep 10 '22 07:09 coryvirok