故障排查 #
故障排查概述 #
Vercel 提供多种工具帮助排查问题:
text
┌─────────────────────────────────────────┐
│ 故障排查工具 │
├─────────────────────────────────────────┤
│ 构建日志 → 排查构建失败 │
│ 函数日志 → 排查运行时错误 │
│ 实时日志 → 实时监控 │
│ CLI 工具 → 本地调试 │
└─────────────────────────────────────────┘
构建问题 #
构建失败常见原因 #
text
┌─────────────────────────────────────────┐
│ 构建失败原因 │
├─────────────────────────────────────────┤
│ 依赖安装失败 │
│ 构建命令错误 │
│ 内存不足 │
│ 环境变量缺失 │
│ TypeScript 错误 │
│ ESLint 错误 │
└─────────────────────────────────────────┘
查看构建日志 #
text
Project → Deployments → [Deployment] → Build Logs
text
[00:00:00.000] Running build in Washington, D.C., USA
[00:00:01.000] Cloning repository...
[00:00:02.000] Cloning completed: 1.234s
[00:00:02.100] Running "vercel build"
[00:00:02.200] Detecting framework...
[00:00:02.300] Framework detected: Next.js
[00:00:02.400] Installing dependencies...
[00:00:15.000] npm install completed
[00:00:15.100] Running "npm run build"
[00:00:45.000] Build failed: Error: ...
依赖问题 #
问题:依赖安装失败
text
npm ERR! ERESOLVE unable to resolve dependency tree
解决方案:
json
{
"overrides": {
"package-name": "version"
}
}
或使用:
bash
npm install --legacy-peer-deps
问题:私有包无法安装
解决方案:
bash
vercel env add NPM_TOKEN
text
.npmrc
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
内存问题 #
问题:构建内存不足
text
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed
解决方案:
json
{
"scripts": {
"build": "NODE_OPTIONS='--max-old-space-size=4096' next build"
}
}
TypeScript 错误 #
问题:类型检查失败
text
Type error: Property 'x' does not exist on type 'Y'.
解决方案:
- 修复类型错误
- 或暂时禁用类型检查:
json
{
"scripts": {
"build": "next build --no-lint"
}
}
ESLint 错误 #
问题:Lint 检查失败
text
Error: ESLint configuration is invalid
解决方案:
检查 .eslintrc.json 配置,或暂时跳过:
json
{
"scripts": {
"build": "next build --no-lint"
}
}
部署问题 #
部署超时 #
问题:部署超时
text
Deployment timed out
解决方案:
- 优化构建速度
- 减少依赖数量
- 使用构建缓存
json
{
"buildCommand": "npm run build",
"installCommand": "npm ci"
}
域名问题 #
问题:域名无法访问
排查步骤:
bash
dig example.com
dig example.com A
dig www.example.com CNAME
检查项:
text
├── DNS 记录是否正确
├── DNS 是否已传播
├── SSL 证书状态
└── 域名是否已验证
SSL 证书问题 #
问题:证书申请失败
text
SSL certificate provisioning failed
解决方案:
- 检查 DNS 配置
- 等待 DNS 传播
- 检查是否有防火墙阻止
404 错误 #
问题:页面返回 404
排查步骤:
- 检查 Output Directory 配置
- 检查路由配置
- 检查文件是否正确生成
json
{
"outputDirectory": "dist"
}
运行时问题 #
函数错误 #
问题:函数执行失败
查看日志:
bash
vercel logs [deployment-url]
常见错误:
text
┌─────────────────────────────────────────┐
│ 函数错误类型 │
├─────────────────────────────────────────┤
│ 超时 → maxDuration 限制 │
│ 内存不足 → memory 限制 │
│ 环境变量 → 变量未设置 │
│ 依赖缺失 → 包未安装 │
└─────────────────────────────────────────┘
函数超时 #
问题:函数执行超时
text
Function execution timed out
解决方案:
typescript
export const maxDuration = 60
环境变量问题 #
问题:环境变量未生效
排查步骤:
bash
vercel env ls
vercel env pull .env.local
检查项:
text
├── 变量是否添加到正确环境
├── 变量名称是否正确
├── 是否需要重新部署
└── 是否有正确的命名前缀
数据库连接问题 #
问题:数据库连接失败
解决方案:
typescript
import { Pool } from 'pg'
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
max: 1,
ssl: {
rejectUnauthorized: false
}
})
性能问题 #
页面加载慢 #
排查步骤:
- 检查 Web Vitals
- 分析网络请求
- 检查资源大小
text
Project → Analytics → Web Vitals
函数响应慢 #
优化方案:
typescript
export const runtime = 'edge'
export async function GET() {
const data = await fetch('https://api.example.com/data', {
next: { revalidate: 60 }
}).then(r => r.json())
return Response.json(data, {
headers: {
'Cache-Control': 's-maxage=60'
}
})
}
图片加载慢 #
优化方案:
tsx
import Image from 'next/image'
export default function Page() {
return (
<Image
src="/hero.jpg"
alt="Hero"
width={800}
height={600}
priority
placeholder="blur"
blurDataURL="data:image/jpeg;base64,..."
/>
)
}
调试技巧 #
本地调试 #
bash
vercel dev
查看详细日志 #
bash
vercel logs --follow
vercel logs --output raw
调试函数 #
typescript
export async function GET() {
console.log('Debug: Request received')
console.log('Debug: Headers', JSON.stringify(request.headers))
try {
const result = await someOperation()
console.log('Debug: Result', JSON.stringify(result))
return Response.json(result)
} catch (error) {
console.error('Debug: Error', error)
return Response.json({ error: error.message }, { status: 500 })
}
}
使用断点 #
typescript
export async function GET() {
debugger
const data = await fetchData()
return Response.json(data)
}
检查部署状态 #
bash
vercel inspect [deployment-url]
常见错误代码 #
| 错误代码 | 说明 | 解决方案 |
|---|---|---|
| 404 | 页面未找到 | 检查路由配置 |
| 500 | 服务器错误 | 查看函数日志 |
| 502 | 网关错误 | 检查函数状态 |
| 503 | 服务不可用 | 检查部署状态 |
| 504 | 网关超时 | 增加超时时间 |
获取帮助 #
Vercel 支持 #
text
┌─────────────────────────────────────────┐
│ 获取帮助途径 │
├─────────────────────────────────────────┤
│ 文档 → vercel.com/docs │
│ 社区论坛 → github.com/vercel/vercel/discussions
│ Twitter → @vercel │
│ 支持 → vercel.com/support │
└─────────────────────────────────────────┘
提交工单 #
text
Dashboard → Help → Contact Support
报告 Bug #
text
github.com/vercel/vercel/issues
预防措施 #
最佳实践 #
text
┌─────────────────────────────────────────┐
│ 故障预防最佳实践 │
├─────────────────────────────────────────┤
│ ✓ 使用 TypeScript │
│ ✓ 配置 ESLint │
│ ✓ 编写测试用例 │
│ ✓ 设置 CI/CD 检查 │
│ ✓ 监控 Web Vitals │
│ ✓ 配置告警通知 │
│ ✓ 定期更新依赖 │
│ ✓ 备份重要配置 │
└─────────────────────────────────────────┘
健康检查 #
typescript
export async function GET() {
const health = {
status: 'ok',
timestamp: new Date().toISOString(),
version: process.env.npm_package_version,
}
return Response.json(health)
}
总结 #
本指南涵盖了 Vercel 常见问题的排查方法。掌握这些技巧可以帮助你快速定位和解决问题,确保项目稳定运行。
恭喜你完成了 Vercel 完全指南的学习!现在你已经掌握了从基础到高级的所有 Vercel 知识,可以开始构建和部署你的项目了!
最后更新:2026-03-28