diff --git a/packages/cloudflare/src/cli/build/bundle-server.ts b/packages/cloudflare/src/cli/build/bundle-server.ts index bcd5e189..ade9c16e 100644 --- a/packages/cloudflare/src/cli/build/bundle-server.ts +++ b/packages/cloudflare/src/cli/build/bundle-server.ts @@ -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"; @@ -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 diff --git a/packages/cloudflare/src/cli/build/patches/to-investigate/patch-load-instrumentation-module.ts b/packages/cloudflare/src/cli/build/patches/to-investigate/patch-load-instrumentation-module.ts new file mode 100644 index 00000000..06d7f0ed --- /dev/null +++ b/packages/cloudflare/src/cli/build/patches/to-investigate/patch-load-instrumentation-module.ts @@ -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(); +}