Deployment Guide: Cloudflare Pages + Workers
Platform: Digital Workspace Ecosystem
Target: Cloudflare Pages + Workers
Last Updated: 2025-10-27
š Why Cloudflare?
Cloudflare Pages adalah pilihan ideal untuk platform ini karena:
- ā Edge Computing: Deploy di edge network worldwide
- ā Zero Config: Automatic deploys dari GitHub
- ā Fast: Global CDN, instant response
- ā Free Tier: Generous free tier untuk open source
- ā Workers Integration: Serverless functions untuk dynamic features
- ā Durable Objects: Untuk future real-time features
- ā R2 Storage: Untuk future file storage needs
š Pre-Deployment Checklist
ā Already Configured
-
@sveltejs/adapter-cloudflareinstalled -
svelte.config.jsconfigured dengan adapter - Markdown files bundled at build time (no filesystem I/O)
- All static assets ready
- No Node.js-specific APIs used
ā ļø Cloudflare-Specific Considerations
No Filesystem Access:
- ā Already fixed - using static imports
- ā All markdown bundled at build time
- ā
No
fs.readFile()at runtime
No Node.js APIs:
- ā Already using Web APIs
- ā
No
fs,pathat runtime - ā Compatible with Workers runtime
Environment Variables:
- Store in Cloudflare Dashboard
- Or use
wrangler.toml(see below)
š ļø Setup Steps
1. Install Wrangler CLI
npm install -g wrangler
# or
pnpm add -D wrangler
2. Login to Cloudflare
wrangler login
3. Configure Wrangler
Create wrangler.toml:
name = "crm-konxc-space"
compatibility_date = "2025-10-27"
pages_build_output_dir = ".svelte-kit/cloudflare"
[build]
command = "pnpm run build"
[site]
bucket = ".svelte-kit/cloudflare"
4. Configure Environment Variables
Create .dev.vars for local development:
DATABASE_URL=your_turso_url
DATABASE_AUTH_TOKEN=your_token
For Production, set in Cloudflare Dashboard:
- Go to Pages project ā Settings ā Environment Variables
- Add:
DATABASE_URL,DATABASE_AUTH_TOKEN
š¢ Deployment Methods
Method 1: GitHub Integration (Recommended)
Connect Repository:
- Go to Cloudflare Dashboard ā Pages
- Click "Create a project"
- Connect GitHub repository
Build Settings:
- Build command:
pnpm run build - Build output directory:
.svelte-kit/cloudflare - Root directory:
/(default)
- Build command:
Environment Variables:
- Set in Cloudflare Dashboard
- Or use
wrangler secret
Deploy:
- Automatic deploy on push to
mainbranch - Preview deploys for PRs
- Automatic deploy on push to
Method 2: Wrangler CLI
# Deploy to production
pnpm run build
wrangler pages deploy .svelte-kit/cloudflare
# Deploy to preview
wrangler pages deploy .svelte-kit/cloudflare --branch=preview
šļø Database: Turso (LibSQL)
Turso Compatibility
ā Compatible with Cloudflare Workers:
- WebSocket-based connection
- HTTP API
- No Node.js specific APIs
Connection String
// src/lib/server/db/connect.ts
import { createClient } from '@libsql/client/web';
export const db = createClient({
url: process.env.DATABASE_URL!,
authToken: process.env.DATABASE_AUTH_TOKEN!
});
Note: Use @libsql/client/web not @libsql/client for Workers!
Update Package.json
pnpm add @libsql/client
Local Development
DATABASE_URL=libsql://your-database.turso.io
DATABASE_AUTH_TOKEN=your-token
š§ Cloudflare-Specific Configurations
vite.config.ts Update
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import cfPages from '@cloudflare/pages-shared';
export default defineConfig({
plugins: [sveltekit(), cfPages()],
ssr: {
noExternal: ['@libsql/client']
}
});
š Build Output
After pnpm run build:
.svelte-kit/
āāā cloudflare/
āāā _app/
ā āāā route-manifest.json
ā āāā layout.js
ā āāā ...
āāā index.html
āāā index.js (worker)
āāā ...
Deploy this .svelte-kit/cloudflare folder to Cloudflare Pages.
š Custom Domain
Add Domain in Cloudflare:
- Pages project ā Custom domains
- Add
crm.konxc.space - DNS will auto-configure
SSL/TLS:
- Automatic HTTPS
- Automatic certificate renewal
šÆ Performance Optimization
Already Optimized ā
- Static imports: All markdown bundled at build time
- Zero runtime I/O: No filesystem access
- Edge deployment: Global CDN
- Code splitting: Automatic by SvelteKit
- Image optimization: Via Cloudflare
Additional Optimizations (Future)
- Use
<link rel="prefetch">for docs navigation - Add service worker for offline docs
- Use Cloudflare Images for image assets
- Use R2 for large file storage
š CI/CD Pipeline
GitHub Actions (optional):
# .github/workflows/deploy.yml
name: Deploy to Cloudflare
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v3
- uses: actions/setup-node@v4
- run: pnpm install
- run: pnpm run build
- uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
š Troubleshooting
Error: "Module not found"
Problem: Node.js modules not available in Workers
Solution: Use Web APIs, not Node.js APIs
Error: "Filesystem not accessible"
Problem: Trying to read files at runtime
Solution: Use static imports, bundle at build time
Error: "Database connection failed"
Problem: Using wrong libsql client
Solution: Use @libsql/client/web not standard client
š Monitoring
Cloudflare Analytics
- Page views
- Response times
- Error rates
- Geographic distribution
Custom Analytics (Future)
- User behavior tracking
- Documentation usage
- Popular pages
š Security
Environment Variables
Store securely in Cloudflare Dashboard:
- ā Never commit to Git
- ā
Use
wrangler secretfor CLI - ā Use Dashboard for Pages deployments
Headers (Optional)
Add in wrangler.toml:
[[pages.headers]]
for = "/*"
[headers.values]
X-Frame-Options = "DENY"
X-Content-Type-Options = "nosniff"
X-XSS-Protection = "1; mode=block"
š Summary
ā Ready for Cloudflare:
- No filesystem I/O
- All assets bundled at build time
- Compatible with Workers runtime
- Edge-optimized
ā Benefits:
- Global CDN
- Fast deployment
- Zero server maintenance
- Generous free tier
ā Next Steps:
- Connect GitHub repo to Cloudflare Pages
- Set environment variables
- Deploy!
Last Updated: 2025-10-27
Status: Ready for Cloudflare Deployment ā