Skip to content

Commit 236c84d

Browse files
committed
define __NEXT_EXPERIMENTAL_REACT to reduce bundle size
1 parent 40fec7d commit 236c84d

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { patchDepdDeprecations } from "./patches/plugins/patch-depd-deprecations
2323
import { fixRequire } from "./patches/plugins/require.js";
2424
import { shimRequireHook } from "./patches/plugins/require-hook.js";
2525
import { setWranglerExternal } from "./patches/plugins/wrangler-external.js";
26-
import { normalizePath, patchCodeWithValidations } from "./utils/index.js";
26+
import { needsExperimentalReact, normalizePath, patchCodeWithValidations } from "./utils/index.js";
2727

2828
/** The dist directory of the Cloudflare adapter package */
2929
const packageDistDir = path.join(path.dirname(fileURLToPath(import.meta.url)), "../..");
@@ -139,7 +139,8 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
139139
"process.env.NEXT_RUNTIME": '"nodejs"',
140140
"process.env.NODE_ENV": '"production"',
141141
"process.env.TURBOPACK": "false",
142-
// Ideally here we should define the `process.env.__NEXT_EXPERIMENTAL_REACT`, but this would require that function splitting is enabled for the case where both versions are needed (i.e. page and app router routes)
142+
// This define should be safe to use for Next 14.2+
143+
"process.env.__NEXT_EXPERIMENTAL_REACT": `${needsExperimentalReact(nextConfig)}`,
143144
},
144145
platform: "node",
145146
banner: {

packages/cloudflare/src/cli/build/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export * from "./apply-patches.js";
22
export * from "./create-config-files.js";
33
export * from "./ensure-cf-config.js";
44
export * from "./extract-project-env-vars.js";
5+
export * from "./needs-experimental-react.js";
56
export * from "./normalize-path.js";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { NextConfig } from "@opennextjs/aws/types/next-types";
2+
3+
// Not sure if this should be upstreamed to aws
4+
// Adding more stuff there make typing incorrect actually, these properties are never undefined as long as it is the right version of next
5+
// Ideally we'd have different `NextConfig` types for different versions of next
6+
interface ExtendedNextConfig extends NextConfig {
7+
experimental: {
8+
ppr?: boolean;
9+
taint?: boolean;
10+
viewTransition?: boolean;
11+
serverActions?: boolean;
12+
};
13+
}
14+
15+
// Copied from https://github.com/vercel/next.js/blob/4518bc91641a0fd938664b781e12ae7c145f3396/packages/next/src/lib/needs-experimental-react.ts#L3-L6
16+
export function needsExperimentalReact(nextConfig: ExtendedNextConfig) {
17+
const { ppr, taint, viewTransition } = nextConfig.experimental || {};
18+
return Boolean(ppr || taint || viewTransition);
19+
}

0 commit comments

Comments
 (0)