Skip to content

Commit

Permalink
Patch the loadInstrumentationModule method to support Next.js 15 (#169
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dario-piotrowicz authored Dec 13, 2024
1 parent 2901d59 commit 429e880
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/cloudflare/src/cli/build/bundle-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { inlineMiddlewareManifestRequire } from "./patches/to-investigate/inline
import { inlineNextRequire } from "./patches/to-investigate/inline-next-require";
import { patchExceptionBubbling } from "./patches/to-investigate/patch-exception-bubbling";
import { patchFindDir } from "./patches/to-investigate/patch-find-dir";
import { patchLoadInstrumentationModule } from "./patches/to-investigate/patch-load-instrumentation-module";
import { patchReadFile } from "./patches/to-investigate/patch-read-file";
import { patchWranglerDeps } from "./patches/to-investigate/wrangler-deps";
import { copyPrerenderedRoutes } from "./utils";
Expand Down Expand Up @@ -164,6 +165,7 @@ async function updateWorkerBundledCode(
patchedCode = await patchCache(patchedCode, openNextOptions);
patchedCode = inlineMiddlewareManifestRequire(patchedCode, config);
patchedCode = patchExceptionBubbling(patchedCode);
patchedCode = patchLoadInstrumentationModule(patchedCode);

patchedCode = patchedCode
// workers do not support dynamic require nor require.resolve
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as ts from "ts-morph";

import { tsParseFile } from "../../utils";

/**
* The `loadInstrumentationModule` method (source: https://github.com/vercel/next.js/blob/5b7833e3/packages/next/src/server/next-server.ts#L301)
* calls `module.findSourceMap` (https://nodejs.org/api/module.html#modulefindsourcemappath) which we haven't implemented causing a runtime error.
*
* To solve this issue this function gets all the `loadInstrumentationModule` declarations found in the file and removes all the statements
* from their bodies (making them no-op methods).
*
* Instrumentation is a Next.js feature for monitoring and logging (see: https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation),
* the removal of this method's logic most likely breaks this feature (see: https://nextjs.org/docs/app/api-reference/file-conventions/instrumentation),
* so this function is likely temporary and something that we'll have to fix in the future.
*
* TODO: investigate and re-enable instrumentation (https://github.com/opennextjs/opennextjs-cloudflare/issues/171)
*/
export function patchLoadInstrumentationModule(code: string) {
const file = tsParseFile(code);
const loadInstrumentationModuleDeclarations = file
.getDescendantsOfKind(ts.SyntaxKind.MethodDeclaration)
.filter((methodDeclaration) => {
if (methodDeclaration.getName() !== "loadInstrumentationModule") {
return false;
}
const methodModifierKinds = methodDeclaration.getModifiers().map((modifier) => modifier.getKind());
if (methodModifierKinds.length !== 1 || methodModifierKinds[0] !== ts.SyntaxKind.AsyncKeyword) {
return false;
}

return true;
});

loadInstrumentationModuleDeclarations.forEach((loadInstrumentationModuleDeclaration) => {
loadInstrumentationModuleDeclaration.setBodyText("");
});

return file.print();
}

0 comments on commit 429e880

Please sign in to comment.