diff --git a/packages/next/src/server/web/spec-extension/request.ts b/packages/next/src/server/web/spec-extension/request.ts index 8f651512c7661..69b6066d36eee 100644 --- a/packages/next/src/server/web/spec-extension/request.ts +++ b/packages/next/src/server/web/spec-extension/request.ts @@ -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, diff --git a/test/unit/web-runtime/next-request.test.ts b/test/unit/web-runtime/next-request.test.ts index 6f441e1bfc0f1..4ab696e11271e 100644 --- a/test/unit/web-runtime/next-request.test.ts +++ b/test/unit/web-runtime/next-request.test.ts @@ -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) + ) +})