Skip to content

Commit 76299bd

Browse files
vicbjames-elicx
andauthored
refactor: group patches for next-server.js (#705)
Co-authored-by: James Anderson <[email protected]>
1 parent 0701070 commit 76299bd

File tree

7 files changed

+253
-151
lines changed

7 files changed

+253
-151
lines changed

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import { getOpenNextConfig } from "../../api/config.js";
1111
import { patchVercelOgLibrary } from "./patches/ast/patch-vercel-og-library.js";
1212
import { patchWebpackRuntime } from "./patches/ast/webpack-runtime.js";
1313
import * as patches from "./patches/index.js";
14-
import { inlineBuildId } from "./patches/plugins/build-id.js";
1514
import { inlineDynamicRequires } from "./patches/plugins/dynamic-requires.js";
1615
import { inlineFindDir } from "./patches/plugins/find-dir.js";
1716
import { patchInstrumentation } from "./patches/plugins/instrumentation.js";
1817
import { inlineLoadManifest } from "./patches/plugins/load-manifest.js";
18+
import { patchNextServer } from "./patches/plugins/next-server.js";
1919
import { handleOptionalDependencies } from "./patches/plugins/optional-deps.js";
2020
import { patchPagesRouterContext } from "./patches/plugins/pages-router-context.js";
2121
import { patchDepdDeprecations } from "./patches/plugins/patch-depd-deprecations.js";
@@ -97,7 +97,7 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
9797
patchPagesRouterContext(buildOpts),
9898
inlineFindDir(updater, buildOpts),
9999
inlineLoadManifest(updater, buildOpts),
100-
inlineBuildId(updater),
100+
patchNextServer(updater, buildOpts),
101101
patchDepdDeprecations(updater),
102102
// Apply updater updates, must be the last plugin
103103
updater.plugin,
@@ -180,11 +180,6 @@ export async function updateWorkerBundledCode(
180180
["require", patches.patchRequire],
181181
["cacheHandler", (code) => patches.patchCache(code, buildOpts)],
182182
["composableCache", (code) => patches.patchComposableCache(code, buildOpts), { isOptional: true }],
183-
[
184-
"'require(this.middlewareManifestPath)'",
185-
(code) => patches.inlineMiddlewareManifestRequire(code, buildOpts),
186-
{ isOptional: true },
187-
],
188183
[
189184
"`require.resolve` call",
190185
// workers do not support dynamic require nor require.resolve
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export * from "./investigated/index.js";
2-
export * from "./to-investigate/inline-middleware-manifest.js";

packages/cloudflare/src/cli/build/patches/plugins/build-id.spec.ts

Lines changed: 0 additions & 85 deletions
This file was deleted.

packages/cloudflare/src/cli/build/patches/plugins/build-id.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
2+
import { describe, expect, test } from "vitest";
3+
4+
import { buildIdRule, createMiddlewareManifestRule } from "./next-server.js";
5+
6+
describe("Next Server", () => {
7+
test("build ID", () => {
8+
const code = `
9+
class NextNodeServer extends _baseserver.default {
10+
constructor(options){
11+
// Initialize super class
12+
super(options);
13+
this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ };
14+
}
15+
async handleUpgrade() {
16+
// The web server does not support web sockets, it's only used for HMR in
17+
// development.
18+
}
19+
loadEnvConfig({ dev, forceReload, silent }) {
20+
(0, _env.loadEnvConfig)(this.dir, dev, silent ? {
21+
info: ()=>{},
22+
error: ()=>{}
23+
} : _log, forceReload);
24+
}
25+
async hasPage(pathname) {
26+
var _this_nextConfig_i18n;
27+
return !!(0, _require.getMaybePagePath)(pathname, this.distDir, (_this_nextConfig_i18n = this.nextConfig.i18n) == null ? void 0 : _this_nextConfig_i18n.locales, this.enabledDirectories.app);
28+
}
29+
getBuildId() {
30+
const buildIdFile = (0, _path.join)(this.distDir, _constants.BUILD_ID_FILE);
31+
try {
32+
return _fs.default.readFileSync(buildIdFile, "utf8").trim();
33+
} catch (err) {
34+
if (err.code === "ENOENT") {
35+
throw new Error(\`Could not find a production build in the '\${this.distDir}' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id\`);
36+
}
37+
throw err;
38+
}
39+
}
40+
getEnabledDirectories(dev) {
41+
const dir = dev ? this.dir : this.serverDistDir;
42+
return {
43+
app: (0, _findpagesdir.findDir)(dir, "app") ? true : false,
44+
pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false
45+
};
46+
}
47+
// ...
48+
}`;
49+
50+
expect(patchCode(code, buildIdRule)).toMatchInlineSnapshot(`
51+
"class NextNodeServer extends _baseserver.default {
52+
constructor(options){
53+
// Initialize super class
54+
super(options);
55+
this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ };
56+
}
57+
async handleUpgrade() {
58+
// The web server does not support web sockets, it's only used for HMR in
59+
// development.
60+
}
61+
loadEnvConfig({ dev, forceReload, silent }) {
62+
(0, _env.loadEnvConfig)(this.dir, dev, silent ? {
63+
info: ()=>{},
64+
error: ()=>{}
65+
} : _log, forceReload);
66+
}
67+
async hasPage(pathname) {
68+
var _this_nextConfig_i18n;
69+
return !!(0, _require.getMaybePagePath)(pathname, this.distDir, (_this_nextConfig_i18n = this.nextConfig.i18n) == null ? void 0 : _this_nextConfig_i18n.locales, this.enabledDirectories.app);
70+
}
71+
getBuildId() {
72+
return process.env.NEXT_BUILD_ID;
73+
}
74+
getEnabledDirectories(dev) {
75+
const dir = dev ? this.dir : this.serverDistDir;
76+
return {
77+
app: (0, _findpagesdir.findDir)(dir, "app") ? true : false,
78+
pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false
79+
};
80+
}
81+
// ...
82+
}"
83+
`);
84+
});
85+
86+
test("middleware manifest", () => {
87+
const code = `
88+
class NextNodeServer extends _baseserver.default {
89+
constructor(options){
90+
// Initialize super class
91+
super(options);
92+
this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ };
93+
}
94+
async handleUpgrade() {
95+
// The web server does not support web sockets, it's only used for HMR in
96+
// development.
97+
}
98+
loadEnvConfig({ dev, forceReload, silent }) {
99+
(0, _env.loadEnvConfig)(this.dir, dev, silent ? {
100+
info: ()=>{},
101+
error: ()=>{}
102+
} : _log, forceReload);
103+
}
104+
async hasPage(pathname) {
105+
var _this_nextConfig_i18n;
106+
return !!(0, _require.getMaybePagePath)(pathname, this.distDir, (_this_nextConfig_i18n = this.nextConfig.i18n) == null ? void 0 : _this_nextConfig_i18n.locales, this.enabledDirectories.app);
107+
}
108+
getBuildId() {
109+
const buildIdFile = (0, _path.join)(this.distDir, _constants.BUILD_ID_FILE);
110+
try {
111+
return _fs.default.readFileSync(buildIdFile, "utf8").trim();
112+
} catch (err) {
113+
if (err.code === "ENOENT") {
114+
throw new Error(\`Could not find a production build in the '\${this.distDir}' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id\`);
115+
}
116+
throw err;
117+
}
118+
}
119+
getMiddlewareManifest() {
120+
if (this.minimalMode) return null;
121+
const manifest = require(this.middlewareManifestPath);
122+
return manifest;
123+
}
124+
getEnabledDirectories(dev) {
125+
const dir = dev ? this.dir : this.serverDistDir;
126+
return {
127+
app: (0, _findpagesdir.findDir)(dir, "app") ? true : false,
128+
pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false
129+
};
130+
}
131+
// ...
132+
}`;
133+
134+
expect(patchCode(code, createMiddlewareManifestRule("manifest"))).toMatchInlineSnapshot(`
135+
"class NextNodeServer extends _baseserver.default {
136+
constructor(options){
137+
// Initialize super class
138+
super(options);
139+
this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ };
140+
}
141+
async handleUpgrade() {
142+
// The web server does not support web sockets, it's only used for HMR in
143+
// development.
144+
}
145+
loadEnvConfig({ dev, forceReload, silent }) {
146+
(0, _env.loadEnvConfig)(this.dir, dev, silent ? {
147+
info: ()=>{},
148+
error: ()=>{}
149+
} : _log, forceReload);
150+
}
151+
async hasPage(pathname) {
152+
var _this_nextConfig_i18n;
153+
return !!(0, _require.getMaybePagePath)(pathname, this.distDir, (_this_nextConfig_i18n = this.nextConfig.i18n) == null ? void 0 : _this_nextConfig_i18n.locales, this.enabledDirectories.app);
154+
}
155+
getBuildId() {
156+
const buildIdFile = (0, _path.join)(this.distDir, _constants.BUILD_ID_FILE);
157+
try {
158+
return _fs.default.readFileSync(buildIdFile, "utf8").trim();
159+
} catch (err) {
160+
if (err.code === "ENOENT") {
161+
throw new Error(\`Could not find a production build in the '\${this.distDir}' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id\`);
162+
}
163+
throw err;
164+
}
165+
}
166+
getMiddlewareManifest() {
167+
return "manifest";
168+
}
169+
getEnabledDirectories(dev) {
170+
const dir = dev ? this.dir : this.serverDistDir;
171+
return {
172+
app: (0, _findpagesdir.findDir)(dir, "app") ? true : false,
173+
pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false
174+
};
175+
}
176+
// ...
177+
}"
178+
`);
179+
});
180+
});

0 commit comments

Comments
 (0)