Thanks to visit codestin.com
Credit goes to github.com

Skip to content

perf: drop @ampproject/toolbox-optimizer to reduce the server bundle size #700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025
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
5 changes: 5 additions & 0 deletions .changeset/four-wombats-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

perf: drop `@ampproject/toolbox-optimizer` to reduce the server bundle size
24 changes: 12 additions & 12 deletions packages/cloudflare/src/cli/build/bundle-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,24 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
] as Plugin[],
external: ["./middleware/handler.mjs"],
alias: {
// Note: it looks like node-fetch is actually not necessary for us, so we could replace it with an empty shim
// but just to be safe we replace it with a module that re-exports the native fetch
// we do this to both save on bundle size (there isn't really any benefit in us shipping the node-fetch code)
// and also get rid of a warning in the terminal caused by the package (because it performs an === comparison with -0)
// 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"),
// Note: we apply an empty shim to next/dist/compiled/ws because it generates two `eval`s:
// eval("require")("bufferutil");
// eval("require")("utf-8-validate");
// Workers have builtin Web Sockets
"next/dist/compiled/ws": path.join(buildOpts.outputDir, "cloudflare-templates/shims/empty.js"),
// Note: we apply an empty shim to next/dist/compiled/edge-runtime since (amongst others) it generated the following `eval`:
// eval(getModuleCode)(module, module.exports, throwingRequire, params.context, ...Object.values(params.scopedContext));
// which comes from https://github.com/vercel/edge-runtime/blob/6e96b55f/packages/primitives/src/primitives/load.js#L57-L63
// The toolbox optimizer pulls severals MB of dependencies (`caniuse-lite`, `terser`, `acorn`, ...)
// Drop it to optimize the code size
// See https://github.com/vercel/next.js/blob/6eb235c/packages/next/src/server/optimize-amp.ts
"next/dist/compiled/@ampproject/toolbox-optimizer": path.join(
buildOpts.outputDir,
"cloudflare-templates/shims/throw.js"
),
// The edge runtime is not supported
"next/dist/compiled/edge-runtime": path.join(
buildOpts.outputDir,
"cloudflare-templates/shims/empty.js"
),
// `@next/env` is a library Next.js uses for loading dotenv files, for obvious reasons we need to stub it here
// source: https://github.com/vercel/next.js/tree/0ac10d79720/packages/next-env
// `@next/env` is used by Next to load environment variables from files.
// OpenNext inlines the values at build time so this is not needed.
"@next/env": path.join(buildOpts.outputDir, "cloudflare-templates/shims/env.js"),
},
define: {
Expand Down
2 changes: 2 additions & 0 deletions packages/cloudflare/src/cli/templates/shims/throw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
throw "OpenNext shim";
export default {};