Skip to content

Commit c0c1d04

Browse files
authored
fix: CustomRequest instantiation (#202)
1 parent d1237fe commit c0c1d04

File tree

7 files changed

+229
-10
lines changed

7 files changed

+229
-10
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
fix: CustomRequest instantiation
6+
7+
In some cases some request properties would not be initialized (i.e. method, headers, ...)
8+
The bug was caused by the processing the init in the CustomRequest class.
9+
The bug was tigerred when using clerk.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function POST(request: Request) {
2+
return new Response(`Hello clerk`);
3+
}

examples/middleware/e2e/base.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ test("matching noop middleware", async ({ page }) => {
2727
expect(page.waitForURL("**/middleware"));
2828
expect(await page.textContent("h1")).toContain("Via middleware");
2929
});
30+
31+
// Test for https://github.com/opennextjs/opennextjs-cloudflare/issues/201
32+
test("clerk middleware", async ({ page }) => {
33+
const res = await page.request.post("/clerk", { data: "some body" });
34+
expect(res.ok()).toEqual(true);
35+
expect(res.status()).toEqual(200);
36+
await expect(res.text()).resolves.toEqual("Hello clerk");
37+
});

examples/middleware/middleware.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
import { NextRequest, NextResponse } from "next/server";
1+
import { NextRequest, NextResponse, NextFetchEvent } from "next/server";
2+
import { clerkMiddleware } from "@clerk/nextjs/server";
23

3-
export function middleware(request: NextRequest) {
4+
export function middleware(request: NextRequest, event: NextFetchEvent) {
45
console.log("middleware");
56
if (request.nextUrl.pathname === "/about") {
67
return NextResponse.redirect(new URL("/redirected", request.url));
78
}
89
if (request.nextUrl.pathname === "/another") {
910
return NextResponse.rewrite(new URL("/rewrite", request.url));
1011
}
12+
if (request.nextUrl.pathname === "/clerk") {
13+
return clerkMiddleware(async () => {}, {
14+
publishableKey: "pk_test_ZXhhbXBsZS5hY2NvdW50cy5kZXYk",
15+
secretKey: "skey",
16+
})(request, event);
17+
}
18+
1119
return NextResponse.next();
1220
}
1321

1422
export const config = {
15-
matcher: ["/about/:path*", "/another/:path*", "/middleware/:path*"],
23+
matcher: ["/about/:path*", "/another/:path*", "/middleware/:path*", "/clerk"],
1624
};

examples/middleware/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"e2e": "playwright test -c e2e/playwright.config.ts"
1313
},
1414
"dependencies": {
15+
"@clerk/nextjs": "6.9.6",
1516
"next": "catalog:",
1617
"react": "catalog:",
1718
"react-dom": "catalog:"

packages/cloudflare/src/cli/build/bundle-server.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,12 @@ fetch = globalThis.fetch;
9494
const CustomRequest = class extends globalThis.Request {
9595
constructor(input, init) {
9696
if (init) {
97-
init = {
98-
...init,
99-
cache: undefined,
100-
// https://github.com/cloudflare/workerd/issues/2746
101-
// https://github.com/cloudflare/workerd/issues/3245
102-
body: init.body instanceof __cf_stream.Readable ? ReadableStream.from(init.body) : init.body,
103-
};
97+
init.cache = undefined;
98+
// https://github.com/cloudflare/workerd/issues/2746
99+
// https://github.com/cloudflare/workerd/issues/3245
100+
Object.defineProperty(init, "body", {
101+
value: init.body instanceof __cf_stream.Readable ? ReadableStream.from(init.body) : init.body;
102+
});
104103
}
105104
super(input, init);
106105
}

0 commit comments

Comments
 (0)