Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/modern-dancers-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@opennextjs/cloudflare": patch
---

fix: vercel og patch not moving to right node_modules directory

There are two separate places where the node_modules could be. One is a package-scoped node_modules which does not always exist - if it doesn't exist, the server functions-scoped node_modules is used.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { patchVercelOgFallbackFont, patchVercelOgImport } from "./vercel-og.js";

type TraceInfo = { version: number; files: string[] };

const vercelOgNodeModulePath = "node_modules/next/dist/compiled/@vercel/og";

/**
* Patches the usage of @vercel/og to be compatible with Cloudflare Workers.
*
Expand All @@ -18,15 +20,16 @@ type TraceInfo = { version: number; files: string[] };
export function patchVercelOgLibrary(buildOpts: BuildOptions) {
const { appBuildOutputPath, outputDir } = buildOpts;

const packagePath = path.join(outputDir, "server-functions/default", getPackagePath(buildOpts));
const functionsPath = path.join(outputDir, "server-functions/default");
const packagePath = path.join(functionsPath, getPackagePath(buildOpts));

for (const traceInfoPath of globSync(path.join(appBuildOutputPath, ".next/server/**/*.nft.json"))) {
const traceInfo: TraceInfo = JSON.parse(readFileSync(traceInfoPath, { encoding: "utf8" }));
const tracedNodePath = traceInfo.files.find((p) => p.endsWith("@vercel/og/index.node.js"));

if (!tracedNodePath) continue;

const outputDir = path.join(packagePath, "node_modules/next/dist/compiled/@vercel/og");
const outputDir = getOutputDir({ functionsPath, packagePath });
const outputEdgePath = path.join(outputDir, "index.edge.js");

// Ensure the edge version is available in the OpenNext node_modules.
Expand Down Expand Up @@ -55,3 +58,12 @@ export function patchVercelOgLibrary(buildOpts: BuildOptions) {
writeFileSync(routeFilePath, node.commitEdits(edits));
}
}

function getOutputDir(opts: { functionsPath: string; packagePath: string }) {
const packageOutputPath = path.join(opts.packagePath, vercelOgNodeModulePath);
if (existsSync(packageOutputPath)) {
return packageOutputPath;
}

return path.join(opts.functionsPath, vercelOgNodeModulePath);
}