Deployment
Deploy your T3 + Cloudflare application to production with Cloudflare Pages.
Prerequisites
Before deploying, ensure you have:
- A Cloudflare account
- Wrangler CLI installed
- Your code pushed to a Git repository (GitHub, GitLab, etc.)
Cloudflare Pages Setup
1. Install Wrangler
If you haven’t already:
npm install -g wrangler2. Login to Cloudflare
wrangler loginThis will open your browser to authenticate with Cloudflare.
3. Create D1 Database
Create a production database:
wrangler d1 create your-database-nameNote the database ID from the output and add it to wrangler.toml:
[[d1_databases]]
binding = "DB"
database_name = "your-database-name"
database_id = "your-database-id"4. Run Database Migrations
Apply your migrations to the production database:
wrangler d1 migrations apply your-database-name --remote5. Create KV Namespace
Create KV namespaces for caching and sessions:
wrangler kv:namespace create "KV"Add the namespace ID to wrangler.toml:
[[kv_namespaces]]
binding = "KV"
id = "your-kv-namespace-id"Build and Deploy
Automated Deployment
The easiest way is to connect your Git repository to Cloudflare Pages:
- Go to Cloudflare Dashboard
- Navigate to Pages
- Click Create a project
- Connect your Git repository
- Configure build settings:
- Build command:
npm run cf:build - Build output directory:
.vercel/output/static - Root directory:
/
- Build command:
Manual Deployment
Deploy manually using Wrangler:
# Build the project
npm run cf:build
# Deploy to Cloudflare Pages
npm run cf:deployOr use the combined command:
wrangler pages deploy .vercel/output/static --project-name your-project-nameEnvironment Variables
Set production environment variables in Cloudflare Dashboard:
- Go to your Pages project
- Navigate to Settings → Environment variables
- Add the following variables:
Required Variables
AUTH_SECRET=your-production-secret
NEXTAUTH_URL=https://yourdomain.com
DATABASE_URL=your-production-db-urlOptional Variables
STRIPE_SECRET_KEY=sk_live_...
STRIPE_WEBHOOK_SECRET=whsec_...
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_live_...
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your-email@gmail.com
SMTP_PASSWORD=your-app-password
AUTH_GITHUB_ID=your-github-id
AUTH_GITHUB_SECRET=your-github-secretCustom Domain
Add Custom Domain
- In Cloudflare Pages, go to Custom domains
- Click Set up a custom domain
- Enter your domain name
- Follow the DNS configuration instructions
SSL Certificate
Cloudflare automatically provisions SSL certificates for custom domains. No additional configuration needed!
Stripe Webhooks
Configure Webhook Endpoint
-
Go to Stripe Dashboard
-
Navigate to Developers → Webhooks
-
Click Add endpoint
-
Enter your webhook URL:
https://yourdomain.com/api/webhooks/stripe -
Select events to listen to:
checkout.session.completedcustomer.subscription.createdcustomer.subscription.updatedcustomer.subscription.deletedinvoice.payment_succeededinvoice.payment_failed
-
Copy the webhook signing secret and add it to your environment variables as
STRIPE_WEBHOOK_SECRET
Monitoring and Logs
View Logs
Check deployment logs in Cloudflare Dashboard:
# Or use Wrangler CLI
wrangler pages deployment tailError Tracking
Consider integrating error tracking services:
- Sentry
- LogRocket
- Datadog
Performance Optimization
Enable Caching
Cloudflare Pages automatically caches static assets. For dynamic content, use:
// In your API routes or pages
export const runtime = "edge";
export const revalidate = 3600; // Cache for 1 hourImage Optimization
Use Next.js Image component with Cloudflare Image Resizing:
import Image from "next/image";
<Image
src="/image.jpg"
alt="Description"
width={800}
height={600}
loader={({ src, width }) => {
return `https://yourdomain.com/cdn-cgi/image/width=${width}/${src}`;
}}
/>;Rollback
If you need to rollback to a previous deployment:
- Go to Cloudflare Pages Dashboard
- Navigate to Deployments
- Find the deployment you want to rollback to
- Click Rollback to this deployment
CI/CD Integration
GitHub Actions
Create .github/workflows/deploy.yml:
name: Deploy to Cloudflare Pages
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
- run: npm ci
- run: npm run cf:build
- name: Deploy to Cloudflare Pages
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
projectName: your-project-name
directory: .vercel/output/staticTroubleshooting
Build Failures
If builds fail:
- Check build logs in Cloudflare Dashboard
- Verify all dependencies are in
package.json - Ensure environment variables are set correctly
Database Connection Issues
If you can’t connect to D1:
- Verify database binding in
wrangler.toml - Check migrations are applied:
wrangler d1 migrations list your-database-name --remote - Ensure
DATABASE_URLpoints to the correct database
500 Errors
For server errors:
- Check Cloudflare Pages logs
- Verify all required environment variables are set
- Test locally with production environment variables
Next Steps
- Authentication Guide - Set up OAuth providers
- Database Guide - Advanced database operations
- API Reference - Complete API documentation