Prerequisites
- A Cloudflare account (sign up here)
- Node.js and pnpm (or npm/yarn) installed
- A TanStack Start application
Installation
Install the required dependencies for Cloudflare Workers deployment:Configuration
1. Update Vite Config
Add the Cloudflare plugin to yourvite.config.ts file:
The
cloudflare plugin must be placed before tanstackStart() in the plugins array.2. Create Wrangler Configuration
Create awrangler.jsonc file in your project root:
3. Update Package Scripts
Modify the scripts in yourpackage.json:
Environment Variables
You can add environment variables to yourwrangler.jsonc file:
Accessing Cloudflare Bindings
You can access Cloudflare bindings (KV, D1, R2, etc.) in your server functions:Type Generation
Generate TypeScript types for your Cloudflare bindings:worker-configuration.d.ts file with type definitions for your environment.
Deployment
Authentication
First, authenticate with your Cloudflare account:Deploy to Production
Deploy your application:- Build your application with Vite
- Run TypeScript type checking
- Deploy to Cloudflare Workers
Advanced Configuration
Custom Routes
Configure custom routes inwrangler.jsonc:
Workers KV
Add KV namespaces to your configuration:D1 Database
Add D1 database bindings:R2 Storage
Add R2 bucket bindings:Preview Deployments
Test your deployment before going to production:Local Development
During development, use Vite’s dev server:Resources
- Cloudflare Workers Documentation
- Wrangler CLI Documentation
- TanStack Start Cloudflare Example
- Cloudflare Platform Bindings
Troubleshooting
Build Errors
If you encounter build errors, ensure:- All dependencies are installed:
pnpm install - TypeScript compiles without errors:
pnpm tsc --noEmit - The
@cloudflare/vite-pluginis placed beforetanstackStart()in plugins
Deployment Errors
If deployment fails:- Check your authentication:
pnpm dlx wrangler whoami - Verify your
wrangler.jsoncconfiguration is valid - Ensure compatibility date is recent
- Check Cloudflare dashboard for quota limits
Runtime Errors
If your app fails at runtime:- Check the Cloudflare Workers logs in your dashboard
- Verify all required bindings are configured
- Ensure
nodejs_compatflag is enabled for Node.js APIs - Test locally with
wrangler devbefore deploying