Skip to content

Commit 429e880

Browse files
Patch the loadInstrumentationModule method to support Next.js 15 (#169)
1 parent 2901d59 commit 429e880

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { inlineMiddlewareManifestRequire } from "./patches/to-investigate/inline
1616
import { inlineNextRequire } from "./patches/to-investigate/inline-next-require";
1717
import { patchExceptionBubbling } from "./patches/to-investigate/patch-exception-bubbling";
1818
import { patchFindDir } from "./patches/to-investigate/patch-find-dir";
19+
import { patchLoadInstrumentationModule } from "./patches/to-investigate/patch-load-instrumentation-module";
1920
import { patchReadFile } from "./patches/to-investigate/patch-read-file";
2021
import { patchWranglerDeps } from "./patches/to-investigate/wrangler-deps";
2122
import { copyPrerenderedRoutes } from "./utils";
@@ -164,6 +165,7 @@ async function updateWorkerBundledCode(
164165
patchedCode = await patchCache(patchedCode, openNextOptions);
165166
patchedCode = inlineMiddlewareManifestRequire(patchedCode, config);
166167
patchedCode = patchExceptionBubbling(patchedCode);
168+
patchedCode = patchLoadInstrumentationModule(patchedCode);
167169

168170
patchedCode = patchedCode
169171
// workers do not support dynamic require nor require.resolve
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as ts from "ts-morph";
2+
3+
import { tsParseFile } from "../../utils";
4+
5+
/**
6+
* The `loadInstrumentationModule` method (source: https://github.com/vercel/next.js/blob/5b7833e3/packages/next/src/server/next-server.ts#L301)
7+
* calls `module.findSourceMap` (https://nodejs.org/api/module.html#modulefindsourcemappath) which we haven't implemented causing a runtime error.
8+
*
9+
* To solve this issue this function gets all the `loadInstrumentationModule` declarations found in the file and removes all the statements
10+
* from their bodies (making them no-op methods).
11+
*
12+
* Instrumentation is a Next.js feature for monitoring and logging (see: https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation),
13+
* the removal of this method's logic most likely breaks this feature (see: https://nextjs.org/docs/app/api-reference/file-conventions/instrumentation),
14+
* so this function is likely temporary and something that we'll have to fix in the future.
15+
*
16+
* TODO: investigate and re-enable instrumentation (https://github.com/opennextjs/opennextjs-cloudflare/issues/171)
17+
*/
18+
export function patchLoadInstrumentationModule(code: string) {
19+
const file = tsParseFile(code);
20+
const loadInstrumentationModuleDeclarations = file
21+
.getDescendantsOfKind(ts.SyntaxKind.MethodDeclaration)
22+
.filter((methodDeclaration) => {
23+
if (methodDeclaration.getName() !== "loadInstrumentationModule") {
24+
return false;
25+
}
26+
const methodModifierKinds = methodDeclaration.getModifiers().map((modifier) => modifier.getKind());
27+
if (methodModifierKinds.length !== 1 || methodModifierKinds[0] !== ts.SyntaxKind.AsyncKeyword) {
28+
return false;
29+
}
30+
31+
return true;
32+
});
33+
34+
loadInstrumentationModuleDeclarations.forEach((loadInstrumentationModuleDeclaration) => {
35+
loadInstrumentationModuleDeclaration.setBodyText("");
36+
});
37+
38+
return file.print();
39+
}

0 commit comments

Comments
 (0)