22.06-Vercel部署

要点

  • Vercel 是 Next.js 的官方平台,前端部署的首选
  • Hono 也可以跑在 Vercel 上,用 @hono/vercel-edge 适配器
  • Vercel 的部署流程:push 到 GitHub → 自动构建 → 自动部署
  • 环境变量、Preview Deployments、Edge Functions 是核心特性

内容

1. Vercel 是什么

Vercel 是 Next.js 的开发公司做的部署平台,主要面向前端和全栈应用:

特性说明
Next.js 优化自动检测 Next.js 项目,零配置部署
Edge Network构建产物部署到全球 CDN
Serverless FunctionsAPI Routes 自动变成 Serverless 函数
Preview Deployments每个 PR 自动部署预览版本
ISR增量静态再生
Analytics内置 Web Vitals 分析

2. Hono 在 Vercel 上跑

Hono 提供了 Vercel 适配器 @hono/vercel-edge

// terminal
npm install @hono/vercel-edge
// api/index.ts
import { Hono } from 'hono'
import { handle } from '@hono/vercel-edge'
 
const app = new Hono()
 
app.get('/', (c) => c.text('Hello Hono on Vercel!'))
app.get('/api/users', (c) => c.json({ users: [] }))
 
export default handle(app)

Vercel 会把 api/ 目录下的文件自动变成 Serverless 函数。

3. Next.js 项目里的 Hono

更常见的用法是在 Next.js 项目里用 Hono 处理部分 API:

// app/api/hono/route.ts
import { Hono } from 'hono'
import { handle } from '@hono/vercel-edge'
 
const app = new Hono()
 
app.get('/', (c) => c.text('Hello from Hono!'))
 
export const GET = handle(app)
export const POST = handle(app)

这样可以在 Next.js 的 App Router 里用 Hono 的路由和中间件。

4. 部署流程

4.1 GitHub 集成

  1. 在 Vercel Dashboard → Add New → Project
  2. 选择 GitHub 仓库和分支
  3. Vercel 自动检测框架(Next.js、Vite 等)
  4. 点击 Deploy

之后每次 push 到 main 自动部署。

4.2 Vercel CLI

// terminal
# 安装
npm install -g vercel
 
# 登录
vercel login
 
# 部署(开发环境)
vercel
 
# 部署(生产环境)
vercel --prod

4.3 CI/CD 中的 Vercel

# .github/workflows/deploy.yml
name: Deploy to Vercel
 
on:
  push:
    branches:
      - main
 
env:
  VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
  VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
  VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
 
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
 
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22
 
      - name: Install Vercel CLI
        run: npm install --global vercel@latest
 
      - name: Pull environment
        run: vercel pull --yes --environment=production
 
      - name: Build
        run: vercel build --prod
 
      - name: Deploy
        run: vercel deploy --prebuilt --prod

5. 环境变量

Vercel 的环境变量在 Dashboard 设置:

  1. Project → Settings → Environment Variables
  2. 添加变量,选择生效环境(Production / Preview / Development)

或者用 CLI:

// terminal
# 添加
vercel env add MY_VAR production
 
# 查看
vercel env ls
 
# 拉取到本地
vercel env pull .env.local

分类

类型前缀用途
公开变量NEXT_PUBLIC_打包到前端 JS,浏览器可见
私有变量无前缀只在 Server Components / API Routes 可用

6. Edge Functions

Vercel 支持把函数部署到边缘网络:

// app/api/edge/route.ts
export const runtime = 'edge'  // 指定为 Edge Runtime
 
export async function GET(request: Request) {
  return new Response('Hello from the edge!')
}

Edge Functions 的限制:

  • 不支持 Node.js 原生 API(fs、net 等)
  • 内存和 CPU 时间有限制
  • 冷启动快(< 100ms)

7. Preview Deployments

Vercel 最有用的特性之一:每个 PR 自动部署一个预览版本。

PR #123 → 自动部署到 https://my-project-xxx.vercel.app

好处:

  1. 代码审查时可以看效果:不用拉下来本地跑
  2. 测试方便:预览版本和生产隔离
  3. 评论和协作:团队成员可以直接在预览上评论

8. 自定义域名

Project → Settings → Domains → 添加域名

Vercel 会告诉你需要配置的 DNS 记录:

  • 根域名:A 记录指向 76.76.21.21
  • 子域名:CNAME 指向 cname.vercel-dns.com

SSL 证书自动申请和续期。

9. 和其他平台的对比

维度VercelNetlifyCloudflare Pages
Next.js 支持最好(官方)
Edge Functions支持支持支持
Serverless支持支持支持(Pages Functions)
免费额度中等中等
Preview Deployments支持支持支持

Vercel 的优势在 Next.js 生态,如果你用 Next.js,Vercel 是最省心的选择。

10. 小结

Vercel 部署的核心:

  1. Next.js 官方平台,部署体验最好
  2. GitHub 集成,push 自动部署
  3. Preview Deployments,每个 PR 有预览版本
  4. Edge Functions,支持边缘计算
  5. Hono 可以用 @hono/vercel-edge 跑在 Vercel 上

Vercel 是前端和 Next.js 项目的首选平台,和 Cloudflare Workers 可以互补——Workers 做后端 API,Vercel 做前端。