Skip to content

Commit 56e9366

Browse files
authored
perf: drop @ampproject/toolbox-optimizer to reduce the server bundle size (#700)
1 parent 49e39ee commit 56e9366

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

.changeset/four-wombats-serve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
perf: drop `@ampproject/toolbox-optimizer` to reduce the server bundle size

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

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

0 commit comments

Comments
 (0)