Skip to content

Edge Function bundle size issues

已部署的 Edge Function 的最大大小取决于它是如何打包的:

🌐 The maximum size of a deployed Edge Function depends on how it's bundled:

  • 本地打包(Supabase CLI): 最大 20 MB。CLI 会在你的电脑上将函数及其依赖打包,然后再上传。
  • 服务器端打包(管理 API 或仪表板): 最大 5 MB。当你在没有本地打包的情况下部署时,打包会在服务器上运行,而服务器的基础设施限制较低。

如果你的函数超过了适用的限制,部署就会失败,并出现类似 Function source code exceeds the maximum deployment size 的错误。

🌐 If your function exceeds the applicable limit, deployment fails with an error such as Function source code exceeds the maximum deployment size.

检查你的包大小 #

🌐 Check your bundle size

使用 deno info 命令来分析你的函数依赖和总大小:

🌐 Use the deno info command to analyze your function's dependencies and total size:

1
deno info /path/to/function/index.ts

看看输出里的“size”字段,就能看到总的包大小。

🌐 Look for the "size" field in the output to see the total bundle size.

如何减小包的大小 #

🌐 How to reduce bundle size

如果你的包太大了,可以试试这些方法:

🌐 If your bundle is too large, try these strategies:

删除未使用的依赖 #

🌐 Remove unused dependencies

检查你的导入,并删除任何你不常用的包。

🌐 Review your imports and remove any packages you're not actively using.

使用选择性导入 #

🌐 Use selective imports

与其导入整个包,不如只导入你需要的具体模块:

🌐 Instead of importing entire packages, import only the specific modules you need:

1
// Good: Import specific submodules
2
import { specific } from 'npm:package/specific'
3
4
// Avoid: Import entire package
5
import * as everything from 'npm:package'

拆分大函数 #

🌐 Split large functions

考虑把大的函数拆分成更小、更专注的函数。每个函数可以处理特定的任务,减少单个部署中需要的代码量。

🌐 Consider breaking large functions into smaller, more focused functions. Each function can handle a specific task, reducing the code needed in any single deployment.

选择轻量化的替代品 #

🌐 Choose lightweight alternatives

研究一些提供相同功能的小型包。许多为 Node.js 设计的 NPM 包包含不必要的 polyfill,会增加打包体积。

🌐 Research smaller packages that provide the same functionality. Many NPM packages designed for Node.js include unnecessary polyfills that increase bundle size.

部署更大的功能 #

🌐 Deploying larger functions

如果你的函数小于 20 MB,但超过了 5 MB 的服务器端限制,可以使用 Supabase CLI 在本地打包以获得更高的限制。运行 supabase functions deploy 并使用 --use-docker 标志来强制本地打包。

🌐 If your function is under 20 MB but exceeds the 5 MB server-side limit, bundle it locally with the Supabase CLI to get the higher limit. Run supabase functions deploy with the --use-docker flag to force local bundling.

额外资源 #

🌐 Additional resources