Skip to content

Commit 54ce502

Browse files
khuezyfwang
andauthored
fix: fix next not using prebundled react library (#106)
* fix: fix next not using prebundled react library * cleanup * cleanup * reset compress * restore compress * Sync --------- Co-authored-by: Frank <[email protected]>
1 parent 2085dac commit 54ce502

File tree

6 files changed

+57
-2
lines changed

6 files changed

+57
-2
lines changed

.changeset/dirty-apricots-shout.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
Set `__NEXT_PRIVATE_PREBUNDLED_REACT` to use prebundled React

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,14 @@ In this case, `path.join(process.cwd(), "posts", "my-post.md")` cannot be resolv
357357

358358
To work around the issue, we change the working directory for the server function to where `.next/` is located, ie. `packages/web`.
359359

360+
#### WORKAROUND: Set `__NEXT_PRIVATE_PREBUNDLED_REACT` to use prebundled React
361+
362+
For Next.js 13.2 and later versions, you need to explicitly set the `__NEXT_PRIVATE_PREBUNDLED_REACT` environment variable. Although this environment variable isn't documented at the time of writing, you can refer to the Next.js source code to understand its usage:
363+
364+
> In standalone mode, we don't have separated render workers so if both app and pages are used, we need to resolve to the prebundled React to ensure the correctness of the version for app.
365+
366+
> Require these modules with static paths to make sure they are tracked by NFT when building the app in standalone mode, as we are now conditionally aliasing them it's tricky to track them in build time.
367+
360368
## Example
361369

362370
In the `example` folder, you can find a Next.js benchmark app. It contains a variety of pages that each test a single Next.js feature. The app is deployed to both Vercel and AWS using [SST](https://docs.sst.dev/start/nextjs).

packages/open-next/src/adapters/image-optimization-adapter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
} from "next/dist/server/image-optimizer";
2222
import { loadConfig, setNodeEnv } from "./util.js";
2323
import { debug } from "./logger.js";
24-
import { convertFrom, isAPIGatewayProxyEvent } from "./event-mapper.js";
2524

2625
setNodeEnv();
2726
const bucketName = process.env.BUCKET_NAME;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// NOTE: add more next config typings as they become relevant
2+
3+
type RemotePattern = {
4+
protocol?: "http" | "https";
5+
hostname: string;
6+
port?: string;
7+
pathname?: string;
8+
};
9+
declare type ImageFormat = "image/avif" | "image/webp";
10+
11+
type ImageConfigComplete = {
12+
deviceSizes: number[];
13+
imageSizes: number[];
14+
path: string;
15+
loaderFile: string;
16+
domains: string[];
17+
disableStaticImages: boolean;
18+
minimumCacheTTL: number;
19+
formats: ImageFormat[];
20+
dangerouslyAllowSVG: boolean;
21+
contentSecurityPolicy: string;
22+
contentDispositionType: "inline" | "attachment";
23+
remotePatterns: RemotePattern[];
24+
unoptimized: boolean;
25+
};
26+
type ImageConfig = Partial<ImageConfigComplete>;
27+
28+
export interface NextConfig {
29+
experimental: {
30+
serverActions?: boolean;
31+
};
32+
images: ImageConfig;
33+
}

packages/open-next/src/adapters/server-adapter.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const openNextDir = path.join(__dirname, ".open-next");
2222
const config = loadConfig(nextDir);
2323
const htmlPages = loadHtmlPages();
2424
const publicAssets = loadPublicAssets();
25+
setNextjsPrebundledReact(config);
2526
debug({ nextDir });
2627

2728
// Create a NextServer
@@ -109,6 +110,14 @@ function setNextjsServerWorkingDirectory() {
109110
process.chdir(__dirname);
110111
}
111112

113+
function setNextjsPrebundledReact(config: any) {
114+
// WORKAROUND: Set `__NEXT_PRIVATE_PREBUNDLED_REACT` to use prebundled React — https://github.com/serverless-stack/open-next#workaround-set-__next_private_prebundled_react-to-use-prebundled-react
115+
process.env.__NEXT_PRIVATE_PREBUNDLED_REACT = config.experimental
116+
.serverActions
117+
? "experimental"
118+
: "next";
119+
}
120+
112121
function loadHtmlPages() {
113122
const filePath = path.join(nextDir, "server", "pages-manifest.json");
114123
const json = fs.readFileSync(filePath, "utf-8");

packages/open-next/src/adapters/util.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "node:fs";
22
import path from "node:path";
3+
import type { NextConfig } from "./next-types.js";
34

45
export function setNodeEnv() {
56
process.env.NODE_ENV = process.env.NODE_ENV ?? "production";
@@ -9,5 +10,5 @@ export function loadConfig(nextDir: string) {
910
const filePath = path.join(nextDir, "required-server-files.json");
1011
const json = fs.readFileSync(filePath, "utf-8");
1112
const { config } = JSON.parse(json);
12-
return config;
13+
return config as NextConfig;
1314
}

0 commit comments

Comments
 (0)