Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/fix-custom-request-constructor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@opennextjs/cloudflare": patch
---

fix: handle plain Request-like objects in `CustomRequest` constructor

workerd's `Request` constructor coerces plain objects to `"[object Object]"` instead of reading `.url`, causing `TypeError: Invalid URL`. The fix extracts `.url` and merges method/headers/body from plain objects before passing to `super()`.
21 changes: 19 additions & 2 deletions packages/cloudflare/src/cli/templates/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ function initRuntime() {
return __original_fetch(input, init);
};

const CustomRequest = class extends globalThis.Request {
// workerd calls toString() on plain objects instead of reading .url,
// so capture the native constructor for instanceof checks.
const OriginalRequest = globalThis.Request;
const CustomRequest = class extends OriginalRequest {
constructor(input: RequestInfo | URL, init?: RequestInit) {
if (init) {
delete (init as { cache: unknown }).cache;
Expand All @@ -88,7 +91,21 @@ function initRuntime() {
value: init.body instanceof stream.Readable ? ReadableStream.from(init.body) : init.body,
});
}
super(input, init);
if (typeof input === "string" || input instanceof URL || input instanceof OriginalRequest) {
super(input, init);
} else {
// Plain Request like object from third-party middleware or edge adapters
// extract .url and merge properties so they aren't lost.
const req = input as unknown as Request;
const merged = {
method: req.method,
headers: req.headers,
body: req.body,
...(req.body ? { duplex: "half" as const } : {}),
...init,
};
super(req.url, merged as RequestInit);
}
}
};

Expand Down