Skip to content

Cloning a Request in edge runtime when running the dev server does not work correctly #57905

@dkokotov

Description

@dkokotov

Link to the code that reproduces this issue

https://github.com/dkokotov/nextjs-edge-request-cloning-bug

To Reproduce

  1. Start the application in development
  2. Navigate to http://localhost:3000
  3. Observe that the method and headers of request 2 do not match those of request 1, even though request 2 was created by cloning request 1 (const req2 = new Request(req1))

Current vs. Expected behavior

Request 2 method and body should be the same as those of request 1.

Note this only happens for edge runtime. If, in the example below copied from the repro repository), I comment out the export const runtime = 'edge', the output is as expected.

export default function Home() {
  const req1 = new Request('https://example.com', {
    method: 'POST',
    headers: { 'X-Example-Header': 'Foo' }
  });
  const req2 = new Request(req1);

  return <div>
    Request 1 method: {req1.method}
    <br/>
    Request 1 headers: {req1.headers}
    <br/>
    Request 2 method: {req2.method}
    <br/>
    Request 2 headers: {req2.headers}
  </div>
}

export const runtime = 'edge';

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:43 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T6000
Binaries:
  Node: 20.8.0
  npm: 10.1.0
  Yarn: 1.22.19
  pnpm: 8.7.5
Relevant Packages:
  next: 14.0.2-canary.5
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

App Router, Middleware / Edge (API routes, runtime)

Additional context

Seems to only happen for running in dev mode. If I deploy the repro example to Vercel (https://nextjs-edge-request-cloning-bug.vercel.app), the output is as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    RuntimeRelated to Node.js or Edge Runtime with Next.js.bugIssue was opened via the bug report template.lockedstaleThe issue has not seen recent activity.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions