Skip to content

fix: new Request(request, init) crashing in route handlers on workerd #1145

Open
ama2369 wants to merge 1 commit intoopennextjs:mainfrom
ama2369:fix/custom-request-constructor
Open

fix: new Request(request, init) crashing in route handlers on workerd #1145
ama2369 wants to merge 1 commit intoopennextjs:mainfrom
ama2369:fix/custom-request-constructor

Conversation

@ama2369
Copy link

@ama2369 ama2369 commented Feb 24, 2026

Summary

Fixes #1144. Likely also fixes #1037.

new Request(request, { headers }) in route handlers crashes with TypeError: Invalid URL: [object Request] because the request object isn't recognized as a native Request by workerd's constructor.

The fix captures OriginalRequest before overwriting globalThis.Request, adds an instanceof guard, and extracts .url with merged properties for non-native objects.

What changed

packages/cloudflare/src/cli/templates/init.ts

  • Capture OriginalRequest before replacement for reliable instanceof checks
  • String, URL, and OriginalRequest instances pass through to super() directly
  • Non-native objects: extract .url, merge method/headers/body into init, set duplex: "half" when body is present

Test plan

  • String and URL inputs still work
  • new Request(request, { headers }) works in route handlers (the bug)
  • Method, headers, and body are preserved through the non-native path
  • stream.Readable body conversion still works (existing behavior)

@changeset-bot
Copy link

changeset-bot bot commented Feb 24, 2026

🦋 Changeset detected

Latest commit: 5f2ef56

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@opennextjs/cloudflare Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ama2369 ama2369 force-pushed the fix/custom-request-constructor branch 2 times, most recently from e68f009 to 9ea1036 Compare February 24, 2026 03:35
@ama2369 ama2369 force-pushed the fix/custom-request-constructor branch from 9ea1036 to 5f2ef56 Compare February 24, 2026 03:37
@ama2369 ama2369 changed the title fix: handle plain Request-like objects in CustomRequest constructor fix: new Request(request, init) crashing in route handlers on workerd Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant