一、是不是 1MB body 限制
错误信息形如:
Error: The Edge Function "api/handler" size is 1.4 MB and your plan size limit is 1 MB.
Learn more: https://vercel.com/docs/concepts/limits/overview
或部署时:
Build error: Edge Function size exceeded
确认你正在使用 Edge Runtime:
// Route Handler
export const runtime = 'edge'
// 或 pages/api(Next.js Pages Router)
export const config = {
runtime: 'edge'
}
如果你的 runtime 是 'nodejs',则不会触发 1MB 限制。
二、最短处理路径(按概率从高到低 5 步)
1:识别大依赖(80% 案例)
ANALYZE=true npm run build
# 或者
npx @next/bundle-analyzer
查看 Edge Function 对应的 chunk 哪个依赖最大。最常见 Top 5:
| 依赖 | 大小 | 替代 |
|---|---|---|
| moment.js | 300KB+ | date-fns / dayjs |
| lodash(全量) | 70KB | lodash-es 按需 / 标准 ES 实现 |
| axios | 70KB | fetch(原生) |
| Prisma Client | 500KB+ | Drizzle / Kysely / Prisma Edge |
| AWS SDK v3 | 1MB+ | aws-sdk/client-{specific} 按服务 |
2:换轻量替代
// ❌ Before
import _ from 'lodash'
const result = _.uniq(arr)
// ✅ After
const result = [...new Set(arr)]
// ❌ Before
import axios from 'axios'
const { data } = await axios.get(url)
// ✅ After
const data = await fetch(url).then(r => r.json())
// ❌ Before
import moment from 'moment'
const formatted = moment(date).format('YYYY-MM-DD')
// ✅ After
import { format } from 'date-fns' // 仅 8KB
const formatted = format(date, 'yyyy-MM-dd')
3:动态 import 异步加载
// ❌ Before (top-level import 拖累 bundle)
import { uploadToS3 } from '@aws-sdk/client-s3'
// ✅ After (dynamic, lazy)
const handler = async (req) => {
const { uploadToS3 } = await import('@aws-sdk/client-s3')
// ...
}
4:升 Vercel Pro 享 4MB
如果代码已经无法再瘦身:
- Vercel Pro $20/月:Edge Function 4MB
- Vercel Enterprise:8MB
5:切回 Node Runtime
如果该接口对冷启动延迟不敏感:
export const runtime = 'nodejs' // 不是 'edge'
Node Runtime 限 50MB,几乎不会触发。代价:冷启动延迟 500-2000ms。
三、为什么会有 1MB 限制(原理)
| 维度 | Edge Runtime | Node Runtime |
|---|---|---|
| 容器 | V8 Isolates | Lambda |
| 启动 | < 50ms | 500-2000ms |
| 内存 | < 128MB | 1024-3008MB |
| 部署 | 200+ 全球节点 | 1-2 region |
| 大小限制 | 1MB(Pro 4MB) | 50MB |
Edge 部署到 200+ 节点,每节点必须快速 cold start,因此 V8 isolate 不能加载大 bundle。1MB 是物理工程权衡。
怎么定位是哪个接口超了 1MB 限制?
4.1 检查 tree-shake 是否生效
# 在 build 输出中查看
.vercel/output/functions/api/handler.func/index.js # 实际部署的 bundle
如果 tree-shake 失败(如 CommonJS 包),bundle 会包含未使用的代码。常见 CommonJS 包问题:
dayjs✅ ESM 友好moment❌ CommonJSlodash❌ CommonJS(用lodash-es代替)
4.2 检查 Polyfills
某些 Next.js 默认 polyfill 在 Edge 不应该有:
// next.config.js
module.exports = {
webpack: (config, { nextRuntime }) => {
if (nextRuntime === 'edge') {
config.resolve.fallback = {
crypto: false,
stream: false,
// ... 禁用 Node-only polyfills
}
}
return config
}
}
4.3 减少 React Component 树
如果你在 Edge 渲染 SSR React,组件树太大也会拖累 bundle:
// ❌ 在 Edge import 整个 UI library
import { Button, Modal, Toast, ... } from '@radix-ui/react-*'
// ✅ 仅在客户端用 UI lib,Edge 只跑 API 逻辑
五、如果## 以上都试过了,下一步查什么?
5.1 改架构
如果业务确实需要大依赖(如 Prisma + Edge),考虑:
- 切分服务:Edge 负责 routing,Node 负责重计算
- 使用 RPC:Edge 调用部署在其他 Node 服务的 endpoint
- 用 Cloudflare Workers + R2/D1 替代 Vercel
5.2 跨境部署联调
调试 Edge Function 大小往往需要反复访问 Vercel Dashboard、Bundle Analyzer 在线版、GitHub Actions CI 日志。
如果你需要稳定登录 Vercel + 跑 CI/CD + 配合 Stripe / Mercury 收款 Dashboard,配一条海外服务跑 GitHub Actions / Cloudflare 的稳定线路能保证部署调试不中断。
六、相关报错
- Vercel 部署
out of memory:Vercel Build Out of Memory 2026 - Vercel 在国内访问慢:Vercel 国内访问慢
- Next.js Server Action payload 限制:Next.js Server Action Payload Too Large
Edge Function size exceeded,查哪一层
Vercel Edge Runtime 1MB 限制 2026,把后台提示、账号邮箱、主体资料和最近一次改动写成时间线;涉及收款或税务时,先停新增改动。
| 步骤 | 要确认什么 |
|---|---|
| 现象 | 是报错、限速、空白页、扣费异常,还是权限不足 |
| 范围 | 只影响一个账号/设备,还是同一批任务都失败 |
| 变更 | 最近是否改过版本、地区、套餐、密钥或规则 |
| 回滚 | 能否回到上一个正常状态并复测 |
什么时候停止继续试错
如果同一问题连续试了三种方案仍无改善,先停下来整理证据。把 Vercel Edge Runtime 1MB 限制 2026 的截图、账号主体和处理人写清楚;涉及收款或签证材料时,先停新增改动再升级处理。独立开发、出海 SaaS 和远程生活 里的很多问题不是单点开关能解决,复盘记录比继续乱改更重要。