Skip to content

Commit 6d196c5

Browse files
committed
fix issue
1 parent c308aed commit 6d196c5

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { patchFetchCacheSetMissingWaitUntil } from "./patches/plugins/fetch-cach
1717
import { inlineFindDir } from "./patches/plugins/find-dir.js";
1818
import { patchInstrumentation } from "./patches/plugins/instrumentation.js";
1919
import { inlineLoadManifest } from "./patches/plugins/load-manifest.js";
20+
import { patchNextMinimal } from "./patches/plugins/next-minimal.js";
2021
import { handleOptionalDependencies } from "./patches/plugins/optional-deps.js";
2122
import { patchDepdDeprecations } from "./patches/plugins/patch-depd-deprecations.js";
2223
import { fixRequire } from "./patches/plugins/require.js";
@@ -99,6 +100,7 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
99100
inlineLoadManifest(updater, buildOpts),
100101
inlineBuildId(updater),
101102
patchDepdDeprecations(updater),
103+
patchNextMinimal(updater),
102104
// Apply updater updaters, must be the last plugin
103105
updater.plugin,
104106
],
@@ -136,7 +138,8 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
136138
// We make sure that environment variables that Next.js expects are properly defined
137139
"process.env.NEXT_RUNTIME": '"nodejs"',
138140
"process.env.NODE_ENV": '"production"',
139-
"process.env.NEXT_MINIMAL": "true",
141+
"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)
140143
},
141144
platform: "node",
142145
banner: {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { patchCode } from "../ast/util.js";
2+
import { ContentUpdater } from "./content-updater.js";
3+
4+
// We try to be as specific as possible to avoid patching the wrong thing here
5+
// It seems that there is a bug in the worker runtime. When the AbortController is created outside of the request context it throws an error (not sure if it's expected or not) except in this case.
6+
// It fails while requiring the `app-page.runtime.prod.js` file, but instead of throwing an error, it just return an empty object for the `require('app-page.runtime.prod.js')` call which makes every request to an app router page fail.
7+
// If it's a bug in workerd and it's not expected to throw an error, we can remove this patch.
8+
export const abortControllerRule = `
9+
rule:
10+
all:
11+
- kind: lexical_declaration
12+
pattern: let $VAR = new AbortController
13+
- precedes:
14+
kind: function_declaration
15+
stopBy: end
16+
has:
17+
kind: statement_block
18+
has:
19+
kind: try_statement
20+
has:
21+
kind: catch_clause
22+
has:
23+
kind: statement_block
24+
has:
25+
kind: return_statement
26+
all:
27+
- has:
28+
stopBy: end
29+
kind: member_expression
30+
pattern: $VAR.signal.aborted
31+
- has:
32+
stopBy: end
33+
kind: call_expression
34+
regex: console.error\\("Failed to fetch RSC payload for
35+
36+
fix:
37+
'let $VAR = {signal:{aborted: false}};'
38+
`
39+
40+
// This rule is used instead of defining `process.env.NEXT_MINIMAL` in the `esbuild config.
41+
// Do we want to entirely replace these functions to reduce the bundle size?
42+
// In next `renderHTML` is used as a fallback in case of errors, but in minimal mode it just throws the error and the responsability of handling it is on the infra.
43+
export const nextMinimalRule = `
44+
rule:
45+
kind: member_expression
46+
pattern: process.env.NEXT_MINIMAL
47+
any:
48+
- inside:
49+
kind: parenthesized_expression
50+
stopBy: end
51+
inside:
52+
kind: if_statement
53+
any:
54+
- inside:
55+
kind: statement_block
56+
inside:
57+
kind: method_definition
58+
any:
59+
- has: {kind: property_identifier, field: name, regex: runEdgeFunction}
60+
- has: {kind: property_identifier, field: name, regex: runMiddleware}
61+
- has: {kind: property_identifier, field: name, regex: imageOptimizer}
62+
- has:
63+
kind: statement_block
64+
has:
65+
kind: expression_statement
66+
pattern: res.statusCode = 400;
67+
fix:
68+
'true'
69+
`
70+
71+
export function patchNextMinimal(updater: ContentUpdater) {
72+
updater.updateContent("patch-abortController-next15.2", { filter: /app-page\.runtime\.prod\.(js)$/, contentFilter: /new AbortController/ }, async ({ contents }) => {
73+
return patchCode(contents, abortControllerRule)
74+
});
75+
76+
updater.updateContent("patch-next-minimal", { filter: /next-server\.(js)$/, contentFilter: /.*/ }, async ({ contents }) => {
77+
return patchCode(contents, nextMinimalRule)
78+
});
79+
80+
return {
81+
name: "patch-abortController",
82+
setup() { },
83+
};
84+
}

0 commit comments

Comments
 (0)