đ Deployment Guide â
Deploy aplikasi SvelteKit ke Cloudflare Pages via GitHub dalam 10 menit.
đ¯ Overview â
Cloudflare Pages dengan Git Integration adalah cara termudah untuk deploy:
- â Auto-deploy - Push ke GitHub = auto deploy
- â Gratis - Unlimited requests, 500 builds/month
- â Global CDN - 300+ lokasi edge
- â Edge Functions - SvelteKit SSR berjalan di edge
- â D1 Integration - Database via Dashboard
đ Pre-Deployment Checklist â
Sebelum deploy, pastikan:
- [ ] Project sudah di-push ke GitHub
- [ ] npm run build berhasil locally
- [ ] Database D1 sudah dibuat di Cloudflare
- [ ] Environment variables sudah disiapkan
- [ ] `wrangler.toml` ada (untuk local dev)đ Deployment Steps (Git Integration) â
Step 1: Buat Project di Dashboard (2 menit) â
Buka Cloudflare Dashboard
Workers & Pages â Create application
Pilih Pages â Connect to Git
Connect GitHub account â Pilih repository kamu
Configure build:
- Project name:
my-app(bebas) - Production branch:
main - Framework preset:
SvelteKit - Build command:
npm run build - Build output directory:
.svelte-kit/cloudflare
- Project name:
Click Save and Deploy
Step 2: Set D1 Database Binding (WAJIB) â
Setelah project terbuat:
- Pilih project kamu â Settings â Bindings
- Click Add â D1 database bindings
- Variable name:
DB - D1 database: Pilih database yang sudah dibuat
- Click Save
â ī¸ Penting
Tanpa setting ini, aplikasi akan error 500 karena tidak bisa connect ke database!
Step 3: Set Environment Variables â
- Settings â Environment variables
- Add variables (Production environment):
Required:
RESEND_API_TOKEN=re_xxxxxxxx
FROM_EMAIL=noreply@yourdomain.comUntuk R2 (jika pakai file upload):
R2_ACCOUNT_ID=xxx
R2_ACCESS_KEY_ID=xxx
R2_SECRET_ACCESS_KEY=xxx
R2_BUCKET_NAME=xxx
R2_PUBLIC_URL=https://pub-xxxxx.r2.devUntuk Google OAuth:
GOOGLE_CLIENT_ID=xxx
GOOGLE_CLIENT_SECRET=xxxStep 4: Redeploy â
- Deployments tab
- Pilih deployment terbaru â Retry deployment
Atau push commit baru ke GitHub:
git commit --allow-empty -m "trigger redeploy"
git pushđ Selesai! Aplikasi sudah online di https://my-app.pages.dev
đī¸ Production Database Migration â
Apply Migration ke Production â
# Apply migration ke D1 production
npm run db:migrateAtau via Wrangler:
npx wrangler d1 migrations apply DB --remoteVerify Production DB â
# Check tables di production
npx wrangler d1 execute DB --remote --command ".tables"đ Custom Domain â
Setup Custom Domain â
- Dashboard â Pages â Project â Custom domains
- Click Set up a custom domain
- Enter domain:
yourdomain.com - Follow DNS setup instructions
DNS Configuration â
Tambahkan DNS records di domain provider:
Type: CNAME
Name: www
Target: my-app.pages.devAtau untuk apex domain:
Type: A
Name: @
Target: 192.0.2.1 (Cloudflare akan provide)đ Production Checklist â
## Pre-Launch Checklist
### Security
- [ ] Environment variables di-set di Cloudflare Dashboard
- [ ] Tidak ada secret di codebase
- [ ] HTTPS enabled (otomatis di Cloudflare)
### Database
- [ ] D1 binding di-set di Dashboard (Settings â Bindings)
- [ ] Migration applied ke production
- [ ] Seed data (jika perlu)
### Features
- [ ] Register/Login working
- [ ] OAuth callback URL updated (production)
- [ ] Email sending working (if used)
- [ ] File upload working (if used)
### Monitoring
- [ ] Analytics enabled
- [ ] Error tracking setup (optional)đ§ Troubleshooting Deployment â
Error 500 Setelah Deploy â
Penyebab #1: D1 Binding Belum Di-set
Solution:
1. Dashboard â Project â Settings â Bindings
2. Add D1 database binding â Variable name: DB
3. Save â RedeployPenyebab #2: Environment Variables Belum Di-set
Solution:
1. Settings â Environment variables
2. Add semua required variables
3. RedeployPenyebab #3: Build Failed
# Check build locally
cd guide/my-app
npm run buildCommon issues:
- TypeScript errors â Run
npm run check - Missing imports â Check case sensitivity
- Node version mismatch â Set di Dashboard: Settings â Build & deployments â Build system version
Error: "D1 binding not found" â
Cek:
- Sudah set binding di Dashboard? (Settings â Bindings)
- Variable name benar? (harus
DB) - Sudah redeploy setelah set binding?
Build System Version â
Jika build failed, cek build system version:
- Dashboard â Project â Settings â Build & deployments
- Build system version: Version 2 (Beta)
- Build command:
npm run build - Build output directory:
.svelte-kit/cloudflare
đ Continuous Deployment â
Dengan Git Integration, setiap push ke branch main akan otomatis deploy ke production.
Preview Deployments â
Push ke branch lain akan membuat preview deployment:
git checkout -b feature/new-page
git push origin feature/new-pagePreview URL: https://feature-new-page.my-app.pages.dev
Deploy Hooks (Optional) â
Untuk trigger deploy dari external:
- Pages â Project â Settings â Build & deployments
- Deploy hooks â Create deploy hook
- Gunakan URL untuk trigger deploy:
curl -X POST https://api.cloudflare.com/client/v4/pages/webhooks/deploy_hooks/xxxxxđ wrangler.toml (Local Development) â
File wrangler.toml diperlukan untuk local development saja. Untuk production, semua binding diatur via Dashboard.
# wrangler.toml - Untuk local development
[[d1_databases]]
binding = "DB"
database_name = "my-app-db"
database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"âšī¸ Info
Untuk Git Integration deployment, wrangler.toml tidak digunakan untuk production binding. Semua binding harus di-set via Dashboard.
đ Monitoring â
Cloudflare Analytics â
Dashboard â Pages â Project â Analytics
Metrics yang tersedia:
- Total requests
- Bandwidth usage
- Build duration
- Error rate
Custom Analytics (Optional) â
Tambahkan tracking script:
<!-- src/routes/+layout.svelte -->
<svelte:head>
<!-- Google Analytics, Plausible, dll -->
</svelte:head>đ¯ Production Best Practices â
1. Environment Variables â
- Jangan commit
.env - Gunakan Cloudflare Dashboard untuk production secrets
- Different values untuk dev vs production
2. Database â
- Always backup sebelum migration besar
- Test migration di local dulu
- Gunakan transactions untuk data integrity
3. Performance â
- Enable Cloudflare caching
- Optimize images (WebP, responsive)
- Minimize JavaScript bundle
4. Security â
- HTTPS only (otomatis di Cloudflare)
- Security headers di
hooks.server.ts:
// src/hooks.server.ts
export const handle = async ({ event, resolve }) => {
const response = await resolve(event);
response.headers.set('X-Frame-Options', 'DENY');
response.headers.set('X-Content-Type-Options', 'nosniff');
response.headers.set('Referrer-Policy', 'strict-origin-when-cross-origin');
return response;
};đ Deployment Complete! â
Aplikasi kamu sekarang:
- â Live di internet
- â Auto-deploy dari GitHub
- â Dihost di 300+ edge locations
- â Dapat HTTPS otomatis
- â Scalable tanpa batas
What's Next? â
- Custom Domain - Gunakan domain sendiri
- Monitoring - Track performance
- AI-First Development - Build fitur baru dengan AI
Selamat! đ Aplikasi sudah live!