Skip to content

Commit

Permalink
fix(edge): allow Request cloning via NextRequest (vercel#53157)
Browse files Browse the repository at this point in the history
### 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 vercel#52967
Closes NEXT-1468
  • Loading branch information
balazsorban44 authored and rfearing committed Jul 26, 2023
1 parent d680975 commit c36d9a2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/next/src/server/web/spec-extension/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions test/unit/web-runtime/next-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
})

0 comments on commit c36d9a2

Please sign in to comment.