Skip to content

Commit c99eefd

Browse files
authored
fix: do not bundle og when not used (#1076)
1 parent 0d45b62 commit c99eefd

4 files changed

Lines changed: 38 additions & 16 deletions

File tree

.changeset/chilly-files-notice.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
fix: do not bundle og when not used
6+
7+
This saves ~500kB when og is not used

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export async function bundleServer(buildOpts: BuildOptions, projectOpts: Project
5959
console.log(`\x1b[35m⚙️ Bundling the OpenNext server...\n\x1b[0m`);
6060

6161
await patchWebpackRuntime(buildOpts);
62-
patchVercelOgLibrary(buildOpts);
62+
const useOg = patchVercelOgLibrary(buildOpts);
6363

6464
const outputPath = path.join(outputDir, "server-functions", "default");
6565
const packagePath = getPackagePath(buildOpts);
@@ -108,7 +108,11 @@ export async function bundleServer(buildOpts: BuildOptions, projectOpts: Project
108108
// Apply updater updates, must be the last plugin
109109
updater.plugin,
110110
] as Plugin[],
111-
external: ["./middleware/handler.mjs"],
111+
external: [
112+
"./middleware/handler.mjs",
113+
// Do not bundle og when it is not used
114+
...(useOg ? [] : ["next/dist/compiled/@vercel/og/index.edge.js"]),
115+
],
112116
alias: {
113117
// Workers have `fetch` so the `node-fetch` polyfill is not needed
114118
"next/dist/compiled/node-fetch": path.join(buildOpts.outputDir, "cloudflare-templates/shims/fetch.js"),

packages/cloudflare/src/cli/build/patches/ast/patch-vercel-og-library.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,28 @@ type TraceInfo = { version: number; files: string[] };
1414
* Patches the usage of @vercel/og to be compatible with Cloudflare Workers.
1515
*
1616
* @param buildOpts Build options.
17+
* @returns Whether the @vercel/og library is used.
1718
*/
18-
export function patchVercelOgLibrary(buildOpts: BuildOptions) {
19+
export function patchVercelOgLibrary(buildOpts: BuildOptions): boolean {
1920
const { appBuildOutputPath, outputDir } = buildOpts;
2021

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

25+
let useOg = false;
26+
2427
for (const traceInfoPath of globSync(path.join(appBuildOutputPath, ".next/server/**/*.nft.json"), {
2528
windowsPathsNoEscape: true,
2629
})) {
27-
let edgeFilePatched = false;
28-
30+
// Look for the Node version of the traced @vercel/og files
2931
const traceInfo: TraceInfo = JSON.parse(readFileSync(traceInfoPath, { encoding: "utf8" }));
3032
const tracedNodePath = traceInfo.files.find((p) => p.endsWith("@vercel/og/index.node.js"));
31-
3233
if (!tracedNodePath) continue;
3334

35+
// If we are here, it means the application is using the @vercel/og library
36+
// and there is an `index.edge.js` colocated file that we need to copy and patch.
37+
useOg = true;
38+
3439
const outputDir = getOutputDir({ functionsPath, packagePath });
3540
const outputEdgePath = path.join(outputDir, "index.edge.js");
3641

@@ -44,12 +49,11 @@ export function patchVercelOgLibrary(buildOpts: BuildOptions) {
4449
copyFileSync(tracedEdgePath, outputEdgePath);
4550
}
4651

47-
if (!edgeFilePatched) {
48-
edgeFilePatched = true;
49-
// Change font fetches in the library to use imports.
50-
const node = parseFile(outputEdgePath);
51-
const { edits, matches } = patchVercelOgFallbackFont(node);
52-
writeFileSync(outputEdgePath, node.commitEdits(edits));
52+
// Change font fetches in the library to use imports.
53+
{
54+
const ast = parseFile(outputEdgePath);
55+
const { edits, matches } = patchVercelOgFallbackFont(ast);
56+
writeFileSync(outputEdgePath, ast.commitEdits(edits));
5357

5458
if (matches.length > 0) {
5559
const fontFileName = matches[0]!.getMatch("PATH")!.text();
@@ -59,12 +63,16 @@ export function patchVercelOgLibrary(buildOpts: BuildOptions) {
5963

6064
// Change node imports for the library to edge imports.
6165
// This is only useful when turbopack is not used to bundle the function.
62-
const routeFilePath = traceInfoPath.replace(appBuildOutputPath, packagePath).replace(".nft.json", "");
66+
{
67+
const routeFilePath = traceInfoPath.replace(appBuildOutputPath, packagePath).replace(".nft.json", "");
6368

64-
const node = parseFile(routeFilePath);
65-
const { edits } = patchVercelOgImport(node);
66-
writeFileSync(routeFilePath, node.commitEdits(edits));
69+
const ast = parseFile(routeFilePath);
70+
const { edits } = patchVercelOgImport(ast);
71+
writeFileSync(routeFilePath, ast.commitEdits(edits));
72+
}
6773
}
74+
75+
return useOg;
6876
}
6977

7078
function getOutputDir(opts: { functionsPath: string; packagePath: string }) {

packages/cloudflare/src/cli/build/patches/ast/vercel-og.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ fix: |-
5454
/**
5555
* Patches the default font fetching to use a .bin import.
5656
*
57+
* We use `.bin` extension as they are added as modules in the wrangler bundler.
58+
* We would need to add a rule to handle `.ttf` otherwise.
59+
*
5760
* @param root Root node.
5861
* @returns Results of applying the rule.
5962
*/

0 commit comments

Comments
 (0)