1. Pengenalan Vercel
Vercel adalah cloud platform yang dirancang khusus untuk frontend developer. Didirikan oleh Guillermo Rauch (pembuat Socket.io dan Mongoose), Vercel adalah perusahaan di balik Next.js β framework React paling populer saat ini. Platform ini menyediakan infrastruktur yang dioptimalkan untuk frontend, memungkinkan developer untuk deploy dengan mudah dan cepat.
Vercel menawarkan pendekatan Git-centric deployment β setiap push ke repository secara otomatis menghasilkan deployment baru. Tidak perlu mengkonfigurasi CI/CD pipeline, Docker, atau server. Cukup hubungkan repository, dan Vercel menangani sisanya.
Mengapa Vercel Populer?
| Keunggulan | Penjelasan |
|---|---|
| Zero-Config Deploy | Auto-detect framework, build, dan deploy tanpa konfigurasi |
| Preview Deployments | Setiap PR mendapatkan URL preview untuk review |
| Edge Network | Global CDN β konten disajikan dari edge terdekat ke user |
| Serverless Functions | Backend API tanpa mengelola server |
| Edge Functions | Code yang berjalan di edge β latency sangat rendah |
| Next.js Optimal | First-class support untuk Next.js β ISR, SSR, SSG, App Router |
| Free Tier | Hobby plan gratis untuk personal project β cukup generous |
| DX Terbaik | Developer experience yang sangat baik β logs, analytics, speed insights |
ββββββββββββ ββββββββββββββββββββββββββββββββββββββββββββββββ
β β β VERCEL PLATFORM β
β Users ββββββΊβ ββββββββββββββββββββββββββββββββββββββββ β
β (Global) βββββββ β EDGE NETWORK (CDN) β β
β β β β ββββββββ ββββββββ ββββββββ β β
ββββββββββββ β β β US β β EU β β APAC β ... β β
β β β Edge β β Edge β β Edge β β β
β β ββββββββ ββββββββ ββββββββ β β
β ββββββββββββββββ¬ββββββββββββββββββββββββ β
β β β
β ββββββββββββββββ΄ββββββββββββββββββββββββ β
β β RUNTIME β β
β β ββββββββββββ ββββββββββββββββββββ β β
β β β Static β β Serverless β β β
β β β Assets β β Functions (Node) β β β
β β β (ISR/SSG)β β Edge Functions β β β
β β ββββββββββββ ββββββββββββββββββββ β β
β ββββββββββββββββββββββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββ
β
ββββββββββββββΌβββββββββββββ
βΌ βΌ βΌ
ββββββββββββ ββββββββββββ ββββββββββββ
β Database β β CMS β β APIs β
β (Planet β β (Content β β (Custom) β
β Scale) β β ful) β β β
ββββββββββββ ββββββββββββ ββββββββββββ
Vercel bukan hanya untuk Next.js β mendukung React, Vue, Svelte, Astro, Nuxt, Remix, Gatsby, dan banyak lagi. Namun integrasi terbaik memang dengan Next.js karena Vercel adalah pengembangnya.
2. Memulai dengan Vercel
Instalasi Vercel CLI
# Install Vercel CLI global npm install -g vercel # Atau gunakan npx (tanpa install global) npx vercel --version # Login ke akun Vercel vercel login # Browser akan terbuka untuk autentikasi # Verifikasi login vercel whoami
Deploy Pertama
# Buat project Next.js baru npx create-next-app@latest my-vercel-app cd my-vercel-app # Deploy ke Vercel (preview) vercel # Deploy ke production vercel --prod # Deploy dengan nama project spesifik vercel --name my-awesome-app --prod # Tampilkan URL deployment vercel ls # Lihat log deployment vercel logs https://my-awesome-app.vercel.app
Deploy dari Git Repository
Cara termudah untuk deploy adalah menghubungkan repository Git. Setiap push akan otomatis trigger deployment.
# Cara 1: Import dari Vercel Dashboard (https://vercel.com/new) # - Pilih repository dari GitHub/GitLab/Bitbucket # - Vercel auto-detect framework settings # - Klik Deploy # Cara 2: Link existing project vercel link # Cara 3: Deploy dari Git dengan Vercel CLI vercel --git-url https://github.com/username/repo
3. Deploy Next.js
Vercel memberikan dukungan terbaik untuk Next.js. Semua fitur Next.js β SSR, SSG, ISR, App Router, Server Components, dan Streaming β berfungsi optimal di Vercel tanpa konfigurasi tambahan.
Routing di Next.js (App Router)
// app/page.tsx β Home page
export default function HomePage() {
return (
<main>
<h1>Selamat Datang di Aplikasi Saya</h1>
<p>Dideploy dengan Vercel π</p>
</>
);
}
// app/about/page.tsx β Static page (SSG)
export default function AboutPage() {
return <h1>Tentang Kami</h1>;
}
// app/blog/[slug]/page.tsx β Dynamic page (SSR/ISR)
interface Props {
params: Promise<{ slug: string }>;
}
export default async function BlogPost({ params }: Props) {
const { slug } = await params;
const post = await fetch(
`https://api.example.com/posts/${slug}`,
{ next: { revalidate: 3600 } } // ISR: revalidate setiap jam
).then(r => r.json());
return (
<article>
<h1>{post.title}</h1>
<p>{post.content}</p>
</article>
);
}
// app/api/users/route.ts β API Route
import { NextRequest, NextResponse } from 'next/server';
export async function GET(request: NextRequest) {
const users = await db.user.findMany();
return NextResponse.json(users);
}
export async function POST(request: NextRequest) {
const body = await request.json();
const user = await db.user.create({ data: body });
return NextResponse.json(user, { status: 201 });
}
Rendering Modes di Vercel
| Mode | Kapan Render | Contoh |
|---|---|---|
| Static (SSG) | Saat build | Halaman About, Landing page |
| SSR | Saat request | Dashboard user, Search results |
| ISR | Saat build, revalidate berkala | Blog post, Product pages |
| Streaming SSR | Progressive streaming | Halaman dengan slow data fetch |
Gunakan Static (SSG) sebisa mungkin untuk performa terbaik. Gunakan ISR untuk konten yang berubah sesekali. Gunakan SSR hanya ketika data harus selalu fresh berdasarkan request user.
4. Serverless Functions
Serverless Functions di Vercel memungkinkan Anda menjalankan backend code tanpa mengelola server. Setiap file di folder api/ (Pages Router) atau app/api/ (App Router) secara otomatis menjadi API endpoint.
Contoh Serverless Function
// app/api/hello/route.ts
import { NextRequest, NextResponse } from 'next/server';
export async function GET(request: NextRequest) {
return NextResponse.json({
message: 'Hello from Vercel Serverless!',
timestamp: new Date().toISOString(),
region: process.env.VERCEL_REGION || 'unknown'
});
}
// app/api/users/[id]/route.ts β Dynamic API route
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params;
const user = await getUserFromDB(id);
if (!user) {
return NextResponse.json(
{ error: 'User not found' },
{ status: 404 }
);
}
return NextResponse.json(user);
}
// app/api/upload/route.ts β File upload handler
export async function POST(request: NextRequest) {
const formData = await request.formData();
const file = formData.get('file') as File;
if (!file) {
return NextResponse.json(
{ error: 'No file uploaded' },
{ status: 400 }
);
}
// Process file...
const bytes = await file.arrayBuffer();
const buffer = Buffer.from(bytes);
return NextResponse.json({
filename: file.name,
size: buffer.length,
type: file.type
});
}
Runtime & Configuration
// vercel.json β Konfigurasi serverless functions
{
"functions": {
"app/api/heavy-task/**": {
"maxDuration": 60,
"memory": 1024
},
"app/api/quick/**": {
"maxDuration": 10,
"memory": 256
}
},
"regions": ["sin1"], // Deploy ke Singapore region
"crons": [
{
"path": "/api/cron/daily-cleanup",
"schedule": "0 0 * * *"
}
]
}
// Runtime Node.js yang tersedia di Vercel:
// - Node.js 18 (default)
// - Node.js 20
// - Python 3.9
// - Go 1.x
// - Ruby 3.2
5. Edge Functions
Edge Functions berjalan di edge network Vercel β dekat dengan user. Berbeda dengan Serverless Functions yang berjalan di satu region, Edge Functions dieksekusi di lebih dari 70 edge locations secara global, menghasilkan latency yang sangat rendah.
Serverless vs Edge Functions
| Aspek | Serverless Functions | Edge Functions |
|---|---|---|
| Runtime | Node.js (full) | Edge Runtime (subset) |
| Lokasi | Satu region | 70+ edge locations |
| Cold Start | π‘ Ada (50-250ms) | π’ Sangat cepat (<50ms) |
| Batas | Hingga 50MB bundle, 256MB RAM | 4MB bundle, 128MB RAM |
| NPM Packages | π’ Semua | π‘ Edge-compatible only |
| Use Case | Complex logic, heavy computation | Auth, A/B testing, geolocation, rewrites |
Contoh Edge Function
// Middleware β Edge Function yang berjalan sebelum setiap request
// middleware.ts (di root project)
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export const config = {
matcher: ['/dashboard/:path*', '/api/:path*']
};
export function middleware(request: NextRequest) {
// Geolocation-based redirect
const country = request.geo?.country || 'US';
const locale = country === 'ID' ? 'id' : 'en';
// Authentication check
const token = request.cookies.get('auth-token')?.value;
if (!token && request.nextUrl.pathname.startsWith('/dashboard')) {
return NextResponse.redirect(new URL('/login', request.url));
}
// A/B testing β 50/50 split
const bucket = request.cookies.get('ab-bucket')?.value;
if (!bucket) {
const newBucket = Math.random() < 0.5 ? 'A' : 'B';
const response = NextResponse.next();
response.cookies.set('ab-bucket', newBucket);
return response;
}
// Add custom headers
const response = NextResponse.next();
response.headers.set('x-user-country', country);
response.headers.set('x-ab-bucket', bucket || 'A');
return response;
}
// app/api/geo/route.ts β Edge API Route
export const runtime = 'edge';
export async function GET(request: NextRequest) {
const { geo, headers } = request;
return new Response(JSON.stringify({
country: geo?.country,
city: geo?.city,
region: geo?.region,
latitude: geo?.latitude,
longitude: geo?.longitude,
ip: headers.get('x-forwarded-for'),
userAgent: headers.get('user-agent')
}), {
headers: { 'Content-Type': 'application/json' }
});
}
6. Preview Deployments
Salah satu fitur terbaik Vercel adalah Preview Deployments. Setiap pull request di GitHub secara otomatis menghasilkan deployment preview dengan URL unik. Ini memungkinkan tim untuk review perubahan secara visual sebelum merge ke production.
Cara Kerja Preview Deployments
ββββββββββββ ββββββββββββββββ ββββββββββββββββββββ
β Developerβ β GitHub β β Vercel β
β βββββΊβ Pull βββββΊβ β
β git push β β Request β β βββββββββββββββ β
β β β β β β Preview β β
β β β ββββββ β Deploy β β
β β β β β β (auto URL) β β
β β β β β ββββββββ¬βββββββ β
β β β β β β β
ββββββββββββ ββββββββββββββββ βββββββββββΌββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββ
β Preview URL: β
β my-app-abc123.vercel.app β
β β
β β
Auto comment di PR β
β β
Lighthouse score β
β β
Screenshot preview β
β β
Web Analytics preview β
βββββββββββββββββββββββββββββββ
ββββββββββββ ββββββββββββββββ ββββββββββββββββββββ
β Team β β Review β β Vercel β
β Member βββββ PR di βββββ β
β βββββΊβ GitHub βββββΊβ Comment otomatis β
β Approve β β β β dengan URL β
ββββββββββββ ββββββββββββββββ ββββββββββββββββββββ
Branch Alias
# Preview deployments mendapatkan URL random
# Contoh: my-app-git-feature-login-username.vercel.app
# Set branch alias untuk URL yang lebih mudah diingat
# Di Vercel Dashboard > Project > Settings > Domains
# atau dengan CLI:
vercel alias set my-app-git-feature-login-username.vercel.app staging.myapp.com
# Set branch-level alias otomatis di vercel.json
{
"git": {
"deploymentEnabled": {
"main": true,
"develop": true,
"feature/*": true
}
}
}
Aktifkan "Protect Preview Deployments" di settings untuk membatasi akses preview hanya ke tim Anda. Ini penting jika preview mengandung data sensitif atau belum siap untuk publik.
7. Environment Variables
Environment variables di Vercel digunakan untuk menyimpan konfigurasi yang berbeda per environment (development, preview, production). Vercel mendukung beberapa jenis variable yang bisa diakses di build time atau runtime.
Jenis Environment Variables
| Jenis | Penggunaan |
|---|---|
| NEXT_PUBLIC_* | Exposed ke client browser (build-time) |
| Server-only | Hanya tersedia di server/API routes (runtime) |
| Encrypted | Ter-enkripsi di Vercel, tidak bisa dilihat lagi |
# Set environment variable via CLI vercel env add DATABASE_URL # Pilih environment: Production, Preview, Development # Set untuk semua environment sekaligus vercel env add API_SECRET production preview development # List semua environment variables vercel env ls # Pull environment variables ke .env.local vercel env pull .env.local # Hapus environment variable vercel env rm DATABASE_URL
Jangan pernah commit .env.local ke Git! Pastikan .env.local ada di .gitignore. Gunakan Vercel Dashboard atau CLI untuk mengelola environment variables di production.
8. Custom Domains
Vercel mendukung custom domains dengan SSL otomatis. Setiap project mendapatkan .vercel.app subdomain gratis, tetapi untuk production sebaiknya gunakan domain sendiri.
Menambah Custom Domain
# Tambah domain via CLI
vercel domains add myapp.com
# Tambah subdomain
vercel domains add blog.myapp.com
# List semua domains
vercel domains ls
# Konfigurasi DNS:
# Type: A Record, Name: @, Value: 76.76.21.21
# Type: CNAME, Name: www, Value: cname.vercel-dns.com
# Wildcard domain (Pro/Enterprise plan)
vercel domains add "*.myapp.com"
# Redirect www ke apex domain
# Di vercel.json:
{
"redirects": [
{ "source": "https://www.myapp.com/:path*", "destination": "https://myapp.com/:path*", "permanent": true }
]
}
9. Caching & ISR
Vercel memiliki caching layer yang sangat canggih yang terintegrasi langsung dengan Next.js. Incremental Static Regeneration (ISR) adalah fitur unggulan yang memungkinkan Anda meng-update konten statis tanpa rebuild seluruh site.
ISR di Next.js
// app/products/[id]/page.tsx β ISR dengan revalidation
export const revalidate = 60; // Revalidate setiap 60 detik
export default async function ProductPage({ params }) {
const { id } = await params;
const product = await fetch(
`https://api.store.com/products/${id}`,
{ next: { revalidate: 60, tags: ['products'] } }
);
return (
<div>
<h1>{product.name}</h1>
<p>Harga: Rp {product.price}</p>
</div>
);
}
// On-demand revalidation via API route
// app/api/revalidate/route.ts
import { revalidateTag } from 'next/cache';
import { NextRequest, NextResponse } from 'next/server';
export async function POST(request: NextRequest) {
const { tag, secret } = await request.json();
if (secret !== process.env.REVALIDATION_SECRET) {
return NextResponse.json({ error: 'Invalid secret' }, { status: 401 });
}
revalidateTag(tag); // 'products'
return NextResponse.json({ revalidated: true, tag });
}
10. Best Practices
Performance
- Gunakan ISR sebisa mungkin β lebih cepat dari SSR, lebih fleksibel dari SSG
- Optimize images β Gunakan
next/imageuntuk auto-optimasi gambar (WebP, resize, lazy load) - Font optimization β Gunakan
next/fontuntuk auto-optimize Google Fonts - Code splitting β Next.js otomatis split code per route, tapi hindari barrel files yang besar
- Edge Functions β Gunakan untuk logic ringan yang butuh latency rendah (auth, redirect, A/B test)
Deployment
- Protect production β Set branch protection di GitHub, deploy ke preview dulu
- Use preview deployments β Review setiap PR secara visual sebelum merge
- Environment variables β Pisahkan config per environment, jangan hardcode secrets
- Monitoring β Aktifkan Vercel Analytics dan Speed Insights untuk production
- Error tracking β Integrasi dengan Sentry atau LogRocket untuk error monitoring
Pricing Tips
| Plan | Harga | Cocok Untuk |
|---|---|---|
| Hobby (Free) | $0/bulan | Personal project, portfolio, belajar |
| Pro | $20/user/bulan | Tim kecil, startup, production apps |
| Enterprise | Custom | Perusahaan besar, SLA, SSO, advanced security |