Skip to content

Commit 59a1d9e

Browse files
make sure that @opennextjs/cloudflare can be used in projects with type set to module (#53)
1 parent 5105d31 commit 59a1d9e

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

examples/api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "api",
33
"version": "0.1.0",
44
"private": true,
5+
"type": "module",
56
"scripts": {
67
"dev": "next dev",
78
"build": "next build",

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import NextNodeServer, { NodeRequestHandler } from "next/dist/server/next-server";
21
import { NodeNextRequest, NodeNextResponse } from "next/dist/server/base-http/node";
32
import { AsyncLocalStorage } from "node:async_hooks";
43
import { type CloudflareContext } from "../../api";
54
import type { IncomingMessage } from "node:http";
65
import { MockedResponse } from "next/dist/server/lib/mock-request";
76
import type { NextConfig } from "next";
7+
import { type NodeRequestHandler } from "next/dist/server/next-server";
88
import Stream from "node:stream";
99

1010
const NON_BODY_RESPONSES = new Set([101, 204, 205, 304]);
@@ -35,6 +35,12 @@ export default {
3535
return cloudflareContextALS.run({ env, ctx, cf: request.cf }, async () => {
3636
if (requestHandler == null) {
3737
globalThis.process.env = { ...globalThis.process.env, ...env };
38+
// Note: "next/dist/server/next-server" is a cjs module so we have to `require` it not to confuse esbuild
39+
// (since esbuild can run in projects with different module resolutions)
40+
// eslint-disable-next-line @typescript-eslint/no-require-imports
41+
const NextNodeServer = require("next/dist/server/next-server")
42+
.default as typeof import("next/dist/server/next-server").default;
43+
3844
requestHandler = new NextNodeServer({
3945
conf: { ...nextConfig, env },
4046
customServer: false,
@@ -58,7 +64,7 @@ export default {
5864

5965
const { req, res, webResponse } = getWrappedStreams(request, ctx);
6066

61-
ctx.waitUntil(requestHandler(new NodeNextRequest(req), new NodeNextResponse(res)));
67+
ctx.waitUntil(requestHandler!(new NodeNextRequest(req), new NodeNextResponse(res)));
6268

6369
return await webResponse();
6470
});

0 commit comments

Comments
 (0)