Cloudflare Pages error 'Functions must be less than 1MB compressed' — what it really means
Key Facts
Direct answer: The direct answer is that Cloudflare Pages enforces a hard limit of 1MB on the size of serverless functions after they have been compressed using Brotli compression, which means your original function code must be significantly smaller than 1MB when compressed.
What the error/limitation actually means: The 'Functions must be less than 1MB compressed' error stems from Cloudflare's underlying infrastructure limitations and performance optimization requirements.
When you'll hit it: You'll encounter this error when developing applications with substantial serverless functions, particularly those that include large libraries, data processing logic, or machine learning models.
How to verify if it applies to you: To check if your functions are approaching this limit, you can inspect the build output during a deployment attempt.
Cloudflare Pages, a popular platform for static site hosting with serverless functions, enforces strict size limitations on function code that often catch developers by surprise. When deploying applications that include serverless functions, developers may encounter an error indicating their function exceeds the 1MB compressed size limit, halting their deployment process and forcing them to reconsider their architecture.
The direct answer is that Cloudflare Pages enforces a hard limit of 1MB on the size of serverless functions after they have been compressed using Brotli compression, which means your original function code must be significantly smaller than 1MB when compressed. This limitation applies to each individual function within your project, not your entire application, and affects all runtimes including JavaScript, TypeScript, and other languages supported by Cloudflare Workers.
What the error/limitation actually means
The 'Functions must be less than 1MB compressed' error stems from Cloudflare's underlying infrastructure limitations and performance optimization requirements. When you deploy a function to Cloudflare Pages, it gets compiled down to a WebAssembly module that runs on Cloudflare's edge network. The 1MB limit isn't arbitrary—it's based on practical constraints of distributing code to thousands of edge locations worldwide while maintaining fast cold start times and efficient memory usage.
Brotli compression, which Cloudflare uses, is a highly efficient compression algorithm that can reduce JavaScript code by 60-80% depending on its content. This means your original uncompressed function code might need to be under 500KB to safely stay under the 1MB compressed limit. The compression happens during the build process, and if the result exceeds 1MB, Cloudflare's build system rejects the deployment with this specific error message. This limitation applies to each function individually, so you could theoretically have multiple functions as long as each one, when compressed, is under 1MB.
When you'll hit it
You'll encounter this error when developing applications with substantial serverless functions, particularly those that include large libraries, data processing logic, or machine learning models. Common scenarios include functions that use heavy dependencies like TensorFlow.js, Pandas.js, or other substantial libraries. For example, a function that processes images or handles complex data transformations might easily exceed this limit, especially when including all necessary npm packages.
Another common situation is when using frameworks or libraries that bundle substantial amounts of code automatically. For instance, if you're using a CMS integration that requires a large client-side library, or if you're implementing complex authentication schemes with multiple dependencies, your function might grow beyond the threshold. Additionally, functions that handle file processing, video transcoding, or other resource-intensive operations often hit this limit because they need to include specialized libraries for these tasks.
How to verify if it applies to you
To check if your functions are approaching this limit, you can inspect the build output during a deployment attempt. Cloudflare Pages will provide detailed error messages indicating which function exceeded the size limit and by how much. You can also manually estimate your function's compressed size by using the Brotli compression algorithm locally or online tools that simulate Cloudflare's compression process.
For a more precise measurement, you can create a test build and examine the output in Cloudflare's dashboard or logs. The build process will report the size of each function after compression. You can also use the wrangler CLI locally to build your project and check the size of generated workers. Run wrangler dev or wrangler deploy with verbose logging to see detailed size information for each function. Additionally, you can inspect the .wrangler directory in your project after a build to find size metrics for your functions.
Your options
Reduce dependencies: Eliminate unnecessary npm packages and replace heavy libraries with lighter alternatives or implement functionality manually.
Split functions: Break down large functions into smaller, specialized functions that can be called sequentially, each staying under the 1MB limit.
Use external APIs: Offload heavy processing to external services or APIs rather than including all code in your function.
Deployxa: Consider using a platform like Deployxa that offers more generous function size limits and optimized build processes for AI applications with substantial codebases.
Common Pitfalls and Troubleshooting
The first pitfall is including development dependencies in your production bundle. Many developers forget that devDependencies can be included in the final bundle if not properly excluded in their build configuration. To fix this, ensure your package.json correctly separates dependencies and that your build process only includes production dependencies. Use npm install --production or similar commands to install only production dependencies before building.
The second pitfall is using large libraries that include unnecessary code. Many JavaScript libraries are tree-shakeable but may not be fully optimized in your build process. To fix this, configure your bundler (like esbuild or webpack) to enable tree-shaking and code splitting. Use dynamic imports to load large libraries only when needed, rather than including them in your main function bundle.
The third pitfall is including assets or data files directly in the function code. Developers sometimes embed large data files or images as base64 strings within their functions, which can quickly consume the size limit. To fix this, store assets externally or use Cloudflare's KV storage for data that needs to be accessed by your functions, then reference it rather than embedding it.
The fourth pitfall is not accounting for the size of dependencies themselves. Some npm packages are notoriously large and can push your function over the limit even before adding your own code. To fix this, audit your dependencies using tools like depcheck or npm audit to identify and replace heavy packages with lighter alternatives. Consider using specialized libraries that focus on minimal size rather than comprehensive feature sets.
The fifth pitfall is misunderstanding how the compression affects your code size. Developers often think their 1.2MB of uncompressed code will be fine, not realizing that Brotli compression might only reduce it to 1.1MB. To fix this, always test your function's compressed size using the same compression algorithm Cloudflare uses. Build your function locally and compress it with Brotli to get an accurate measurement before deploying.
Conclusion
Understanding and working within Cloudflare Pages' 1MB function limit requires careful attention to your dependencies, build configuration, and architecture decisions. By breaking down large functions, optimizing your dependencies, and leveraging external resources, you can build powerful applications that stay within these constraints. For applications that genuinely require larger functions, exploring alternative platforms that offer more generous limits might be necessary.
To learn more about optimizing your serverless functions and exploring deployment options, review Cloudflare's official documentation on function limits and best practices. For developers working with AI applications or complex data processing, researching platforms designed specifically for these workloads can provide additional flexibility and performance benefits.