Skip to content

Commit 5f2ef56

Browse files
committed
fix: handle plain Request-like objects in CustomRequest constructor
1 parent 5ca6a92 commit 5f2ef56

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
fix: handle plain Request-like objects in `CustomRequest` constructor
6+
7+
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()`.

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ function initRuntime() {
7777
return __original_fetch(input, init);
7878
};
7979

80-
const CustomRequest = class extends globalThis.Request {
80+
// workerd calls toString() on plain objects instead of reading .url,
81+
// so capture the native constructor for instanceof checks.
82+
const OriginalRequest = globalThis.Request;
83+
const CustomRequest = class extends OriginalRequest {
8184
constructor(input: RequestInfo | URL, init?: RequestInit) {
8285
if (init) {
8386
delete (init as { cache: unknown }).cache;
@@ -88,7 +91,21 @@ function initRuntime() {
8891
value: init.body instanceof stream.Readable ? ReadableStream.from(init.body) : init.body,
8992
});
9093
}
91-
super(input, init);
94+
if (typeof input === "string" || input instanceof URL || input instanceof OriginalRequest) {
95+
super(input, init);
96+
} else {
97+
// Plain Request like object from third-party middleware or edge adapters
98+
// extract .url and merge properties so they aren't lost.
99+
const req = input as unknown as Request;
100+
const merged = {
101+
method: req.method,
102+
headers: req.headers,
103+
body: req.body,
104+
...(req.body ? { duplex: "half" as const } : {}),
105+
...init,
106+
};
107+
super(req.url, merged as RequestInit);
108+
}
92109
}
93110
};
94111

0 commit comments

Comments
 (0)