Skip to content

Commit 56127f5

Browse files
committed
feat: use incremental cache during rendering
1 parent bcaaec6 commit 56127f5

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

packages/cloudflare/src/cli/build/build-worker.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Config } from "../config";
55
import { copyPackageCliFiles } from "./patches/investigated/copy-package-cli-files";
66
import { fileURLToPath } from "node:url";
77
import { inlineEvalManifest } from "./patches/to-investigate/inline-eval-manifest";
8+
import { inlineMiddlewareManifestRequire } from "./patches/to-investigate/inline-middleware-manifest-require";
89
import { inlineNextRequire } from "./patches/to-investigate/inline-next-require";
910
import { patchCache } from "./patches/investigated/patch-cache";
1011
import { patchFindDir } from "./patches/to-investigate/patch-find-dir";
@@ -90,10 +91,6 @@ export async function buildWorker(config: Config): Promise<void> {
9091
// Note: we need the __non_webpack_require__ variable declared as it is used by next-server:
9192
// https://github.com/vercel/next.js/blob/be0c3283/packages/next/src/server/next-server.ts#L116-L119
9293
__non_webpack_require__: "require",
93-
// The next.js server can run in minimal mode: https://github.com/vercel/next.js/blob/aa90fe9bb/packages/next/src/server/base-server.ts#L510-L511
94-
// this avoids some extra (/problematic) `require` calls, such as here: https://github.com/vercel/next.js/blob/aa90fe9bb/packages/next/src/server/next-server.ts#L1259
95-
// that's wht we enable it
96-
"process.env.NEXT_PRIVATE_MINIMAL_MODE": "true",
9794
// Ask mhart if he can explain why the `define`s below are necessary
9895
"process.env.NEXT_RUNTIME": '"nodejs"',
9996
"process.env.NODE_ENV": '"production"',
@@ -166,6 +163,7 @@ async function updateWorkerBundledCode(workerOutputFile: string, config: Config)
166163
patchedCode = patchFindDir(patchedCode, config);
167164
patchedCode = inlineEvalManifest(patchedCode, config);
168165
patchedCode = patchCache(patchedCode, config);
166+
patchedCode = inlineMiddlewareManifestRequire(patchedCode, config);
169167

170168
await writeFile(workerOutputFile, patchedCode);
171169
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { existsSync, readFileSync } from "node:fs";
2+
import { Config } from "../../../config";
3+
import path from "node:path";
4+
5+
/**
6+
* Inlines the middleware manifest from the build output to prevent a dynamic require statement
7+
* as they result in runtime failures.
8+
*/
9+
export function inlineMiddlewareManifestRequire(code: string, config: Config) {
10+
console.log("# inlineMiddlewareManifestRequire");
11+
12+
const middlewareManifestPath = path.join(config.paths.standaloneAppServer, "middleware-manifest.json");
13+
14+
const middlewareManifest = existsSync(middlewareManifestPath)
15+
? JSON.parse(readFileSync(middlewareManifestPath, "utf-8"))
16+
: {};
17+
18+
return code.replace(/require\(this.middlewareManifestPath\)/, JSON.stringify(middlewareManifest));
19+
}

0 commit comments

Comments
 (0)