Skip to content

Commit a2679bf

Browse files
authored
fix(cloudflare): stop bundling @vercel/og when it is unused (#1221)
Co-authored-by: mushan0x0 <mushan0x0@users.noreply.github.com>
1 parent d577521 commit a2679bf

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
Stop bundling `@vercel/og` (and its ~1.4 MiB `resvg.wasm`) when the app does not use it.
6+
7+
Next.js's `externalImport` helper keeps a dynamic `import("next/dist/compiled/@vercel/og/index.edge.js")` in the emitted handler even for apps that never use `ImageResponse` / `opengraph-image`. Previously this module was marked as `external` when `useOg` was `false`, which left Wrangler to resolve and bundle it — pulling in ~800 KiB of JS plus `resvg.wasm` and pushing many Workers over the Cloudflare free-tier 3 MiB gzip limit.
8+
9+
When `useOg` is `false`, the edge entry is now aliased to the existing `throw.js` shim, so the unreachable dynamic import resolves to a tiny module and the real `@vercel/og` library is no longer pulled into the Worker bundle.

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,20 @@ export async function bundleServer(buildOpts: BuildOptions, projectOpts: Project
113113
// Apply updater updates, must be the last plugin
114114
updater.plugin,
115115
] as Plugin[],
116-
external: [
117-
"./middleware/handler.mjs",
118-
// Do not bundle og when it is not used
119-
...(useOg ? [] : ["next/dist/compiled/@vercel/og/index.edge.js"]),
120-
],
116+
external: ["./middleware/handler.mjs"],
121117
alias: {
118+
// When @vercel/og is not used, alias the edge entry to a throwing shim so the
119+
// dynamic `import("next/dist/compiled/@vercel/og/index.edge.js")` call site
120+
// emitted by Next.js does not drag the library (~800 KiB) and its
121+
// `resvg.wasm` (~1.4 MiB) into the Worker bundle.
122+
...(useOg
123+
? {}
124+
: {
125+
"next/dist/compiled/@vercel/og/index.edge.js": path.join(
126+
buildOpts.outputDir,
127+
"cloudflare-templates/shims/throw.js"
128+
),
129+
}),
122130
// Workers have `fetch` so the `node-fetch` polyfill is not needed
123131
"next/dist/compiled/node-fetch": path.join(buildOpts.outputDir, "cloudflare-templates/shims/fetch.js"),
124132
// Workers have builtin Web Sockets

0 commit comments

Comments
 (0)