Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions .changeset/drop-unused-vercel-og.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@opennextjs/cloudflare": patch
---

Stop bundling `@vercel/og` (and its ~1.4 MiB `resvg.wasm`) when the app does not use it.

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.

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.
18 changes: 13 additions & 5 deletions packages/cloudflare/src/cli/build/bundle-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,20 @@ export async function bundleServer(buildOpts: BuildOptions, projectOpts: Project
// Apply updater updates, must be the last plugin
updater.plugin,
] as Plugin[],
external: [
"./middleware/handler.mjs",
// Do not bundle og when it is not used
...(useOg ? [] : ["next/dist/compiled/@vercel/og/index.edge.js"]),
],
external: ["./middleware/handler.mjs"],
alias: {
// When @vercel/og is not used, alias the edge entry to a throwing shim so the
// dynamic `import("next/dist/compiled/@vercel/og/index.edge.js")` call site
// emitted by Next.js does not drag the library (~800 KiB) and its
// `resvg.wasm` (~1.4 MiB) into the Worker bundle.
...(useOg
? {}
: {
"next/dist/compiled/@vercel/og/index.edge.js": path.join(
buildOpts.outputDir,
"cloudflare-templates/shims/throw.js"
),
}),
// Workers have `fetch` so the `node-fetch` polyfill is not needed
"next/dist/compiled/node-fetch": path.join(buildOpts.outputDir, "cloudflare-templates/shims/fetch.js"),
// Workers have builtin Web Sockets
Expand Down
Loading