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

fix(edge): allow `Request` cloning via `NextRequest` (#53157)

### What?

Allow the following:

```ts
new NextRequest(new Request(...))
```

### Why?

Cloning a request by passing it to the constructor of another `Request` is allowed by the spec: https://developer.mozilla.org/en-US/docs/Web/API/Request/Request#parameters

### How?

If the passed argument is an instance of `Request`, we pass it as-is to `super()`

Fixes #52967
Closes NEXT-1468
parent d6809755
......@@ -20,7 +20,8 @@ export class NextRequest extends Request {
const url =
typeof input !== 'string' && 'url' in input ? input.url : String(input)
validateURL(url)
super(url, init)
if (input instanceof Request) super(input)
else super(url, init)
const nextUrl = new NextURL(url, {
headers: toNodeOutgoingHttpHeaders(this.headers),
nextConfig: init.nextConfig,
......
......@@ -21,3 +21,15 @@ it('should allow the 2nd parameter to be undefined', () => {
'/'
)
})
it('should clone Request with headers', () => {
const request = new Request('https://example.com', {
headers: { 'x-foo': 'bar' },
})
const nextRequest = new NextRequest(request)
expect(Object.fromEntries(nextRequest.headers)).toEqual(
Object.fromEntries(request.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