Unverified Commit af97755e authored by Balázs Orbán's avatar Balázs Orbán Committed by GitHub

fix(edge): override init when cloning with `NextRequest` (#54108)

### What?

When cloning a `Request` with `NextRequest` the second argument needs to take precedence over the `Headers` set on the original request.

### Why?

Follow-up of #53157

I checked the `Request` spec https://fetch.spec.whatwg.org/#request-class and following the order of execution, `init.headers` should indeed override the headers if the first param was `Request`.

I verified this in the browser too:

```js
const req1 = new Request("http://n", {headers: {"x-header": "foo"}})
const req2 = new Request(req1, {headers: {"x-header-2": "bar"}})
Object.fromEntries(req2.headers) // {x-header-2: "bar"}
```

So we should match `NextRequest` with this behavior.

### How?

Pass the `init` to `super` when cloning the request.

Closes NEXT-1521
Fixes #54094
parent 96dd8ff4
......@@ -20,7 +20,7 @@ export class NextRequest extends Request {
const url =
typeof input !== 'string' && 'url' in input ? input.url : String(input)
validateURL(url)
if (input instanceof Request) super(input)
if (input instanceof Request) super(input, init)
else super(url, init)
const nextUrl = new NextURL(url, {
headers: toNodeOutgoingHttpHeaders(this.headers),
......
......@@ -32,4 +32,12 @@ it('should clone Request with headers', () => {
expect(Object.fromEntries(nextRequest.headers)).toEqual(
Object.fromEntries(request.headers)
)
// Second argument should override headers
const headers = new Headers({ 'x-header': 'some header' })
const nextRequest2 = new NextRequest(request, { headers })
expect(Object.fromEntries(nextRequest2.headers)).toEqual(
Object.fromEntries(headers)
)
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment