Cloudflare Workers Deployment
Guide to deploying TanStack Start app to Cloudflare Workers
Prerequisites
- Have a Cloudflare account
- Dependencies are installed (
wranglerand@cloudflare/vite-pluginare already indevDependencies)
Step 1: Log in to Cloudflare
cd apps/tanstack-app
npx wrangler loginThis will open a browser for authorization. After success, the terminal shows Successfully logged in.
Step 2: Configure Hyperdrive (Database)
First, create Hyperdrive with your PostgreSQL connection string (this proxy is used by online Workers):
npx wrangler hyperdrive create tinyship-db \
--connection-string="postgresql://user:password@host:5432/dbname"After creation, you will get a Hyperdrive ID. Then configure both fields in wrangler.jsonc:
id: used for production deployment (Cloudflare edge accesses DB via Hyperdrive afterwrangler deploy)localConnectionString: used for local development (wrangler dev/pnpm dev:cfconnects directly to your local/LAN DB)
Example (matching current project structure):
"hyperdrive": [
{
"binding": "HYPERDRIVE",
"id": "<your-hyperdrive-id>",
"localConnectionString": "postgresql://viking:unused@localhost:5432/shipeasy_dev"
}
]Notes:
- Local debugging prefers
localConnectionString, which is easier for local DB access and avoids depending on remote network. - After deploying to Cloudflare,
localConnectionStringis not used; theidHyperdrive config is used. - If
localConnectionStringis not set, local CF debugging goes through remote Hyperdrive path, which is usually slower and more internet-dependent.
Step 3: Configure Secrets
Sensitive configuration in Cloudflare Workers should use Secrets (not .env):
# Required
npx wrangler secret put BETTER_AUTH_SECRET
npx wrangler secret put DATABASE_URL
# Add more secrets as needed
# 2) You can also bulk import from a file
npx wrangler secret bulk .secrets.jsonRecommendation: use
.secrets.jsononly for local temporary import, and do not commit it to Git.
Non-sensitive variables should be put in vars in wrangler.jsonc:
"vars": {
"APP_BASE_URL": "https://your-domain.com",
"BETTER_AUTH_URL": "https://your-domain.com"
}Step 4: Local Preview
Validate first in local Workers simulation:
cd apps/tanstack-app
pnpm dev:cfThis command runs Vite + Cloudflare plugin with CF_DEPLOY=1, simulating Workers runtime. Recommended checks:
- The app opens on the output URL (usually
http://localhost:7001) - Sign in / sign up works
- DB-dependent features load correctly
- API endpoints return expected responses
Step 5: Deploy to Production
cd apps/tanstack-app
pnpm run deploy:cfThis runs CF_DEPLOY=1 vite build && wrangler deploy. On success, it prints an address like:
https://tinyship-tanstack.<your-subdomain>.workers.devStep 6: Post-deploy Verification
Verify these endpoints on deployed URL:
| Check | URL | Expected |
|---|---|---|
| Health check | /api/health | 200 OK |
| Home | /en | Page renders normally |
| Sign-in page | /en/signin | Page is accessible |
| DB feature | /api/credits/balance (requires session) | Returns balance correctly |
# Quick smoke test
curl -s -o /dev/null -w "%{http_code}" https://your-worker-url/api/healthHow CF Mode Works (Developer Notes)
Cloudflare integration uses an opt-in approach and does not affect normal development:
┌─────────────────────────────────────────────────┐
│ pnpm dev (normal dev) │
│ → Vite dev server, Node.js, DATABASE_URL │
│ → CF_DEPLOY is not set │
│ → cloudflare() plugin is not loaded │
├─────────────────────────────────────────────────┤
│ pnpm dev:cf (CF local preview) │
│ → CF_DEPLOY=1 → load cloudflare() plugin │
│ → Vite + Cloudflare plugin simulates Workers │
│ → Uses Hyperdrive or DATABASE_URL │
├─────────────────────────────────────────────────┤
│ pnpm run deploy:cf (production deploy) │
│ → CF_DEPLOY=1 → load cloudflare() plugin │
│ → vite build → workers-compatible output │
│ → wrangler deploy → publish to edge network │
└─────────────────────────────────────────────────┘Key mechanism: vite.config.ts checks process.env.CF_DEPLOY, and dynamically imports @cloudflare/vite-plugin only when present. This means:
pnpm dev/pnpm build: pure Vite/Node.js flow, no CF overheadpnpm dev:cf/pnpm run deploy:cf: Workers-compatible build flow
Comparison with Other Deployment Options
| Feature | Cloudflare Workers | Docker | Vercel | Traditional |
|---|---|---|---|---|
| Supported framework | TanStack Start | All | Next.js | All |
| Cold start | < 5ms | None (always-on) | ~250ms | None |
| Global distribution | Automatic | Manual | Automatic | Manual |
| Free quota | 100k requests/day | None | Limited | None |
| Database support | Hyperdrive required | Native TCP | Native TCP | Native TCP |
| Upload limit | 100MB / 500MB | Unlimited | 50MB | Unlimited |
| Ops cost | Very low | Medium | Low | High |
Cloudflare Workers is great for scenarios requiring high performance, global coverage, and low ops cost. If you're concerned about DB connectivity model or runtime limits, Docker or traditional deployment may be simpler.