Leadership-Legacy-Startup-Agency-Website

Cloudflare Deployment Guide

Leadership Legacy - Complete Setup Instructions

This guide covers the complete deployment process for migrating Leadership Legacy to Cloudflare infrastructure with Clay-inspired design enhancements.


Table of Contents

  1. Prerequisites
  2. Initial Cloudflare Setup
  3. Database Configuration
  4. Storage Configuration
  5. Workers Deployment
  6. Pages Deployment
  7. Local Development
  8. Environment Variables
  9. Troubleshooting

Prerequisites

Before starting, ensure you have:


Initial Cloudflare Setup

A ready-to-use CI workflow lives at .github/workflows/cloudflare.yml. Add the following repository secrets so pushes to main automatically build the static Next.js export, deploy the Worker API, and publish Cloudflare Pages:

Local environment variables are documented in .env.example (commit-safe).

1. Install Dependencies

npm install

2. Configure Wrangler CLI

Wrangler is already installed as a dev dependency. Authenticate with Cloudflare:

npx wrangler login

This will open a browser window for authentication. Alternatively, set the API token manually:

export CLOUDFLARE_API_TOKEN=SRt4Hz-Sk78lrbN0LMW49LkK_xGbE5CEeqT41_1e
export CLOUDFLARE_ACCOUNT_ID=d3dbdd3e1ebbc28edf0ce756d9841490

Or add to your shell profile (~/.bashrc, ~/.zshrc):

echo 'export CLOUDFLARE_API_TOKEN=SRt4Hz-Sk78lrbN0LMW49LkK_xGbE5CEeqT41_1e' >> ~/.bashrc
echo 'export CLOUDFLARE_ACCOUNT_ID=d3dbdd3e1ebbc28edf0ce756d9841490' >> ~/.bashrc
source ~/.bashrc

Database Configuration

1. Create D1 Database

npm run cf:db:create

This creates a D1 SQLite database named leadership-legacy-db.

Expected Output:

✅ Successfully created DB 'leadership-legacy-db'
Database ID: [your-database-id]

2. Update wrangler.toml

Copy the Database ID from the output above and update wrangler.toml:

[[d1_databases]]
binding = "DB"
database_name = "leadership-legacy-db"
database_id = "YOUR_DATABASE_ID_HERE"  # ← Replace this

3. Run Database Migrations

npm run cf:db:migrate

This executes the SQL schema in schema/schema.sql, creating tables for:

Verify Tables:

npx wrangler d1 execute leadership-legacy-db --command="SELECT name FROM sqlite_master WHERE type='table';"

Storage Configuration

1. Create R2 Bucket (Object Storage)

npm run cf:r2:create

This creates an R2 bucket named leadership-legacy-assets for storing:

2. Create KV Namespace (Key-Value Store)

npm run cf:kv:create

This creates a KV namespace for:

Expected Output:

🌀 Creating namespace...
✅ Success!
Add the following to your configuration file:
kv_namespaces = [
  { binding = "LEADERSHIP_CONFIG", id = "YOUR_KV_ID" }
]

3. Update wrangler.toml

Add the KV namespace ID to wrangler.toml:

[[kv_namespaces]]
binding = "CONFIG"
id = "YOUR_KV_ID_HERE"  # ← Replace this

Workers Deployment

1. Deploy API Workers

npm run cf:deploy

This deploys the Workers in workers/api/index.ts with the following endpoints:

2. Verify Deployment

curl https://leadership-legacy.YOUR_SUBDOMAIN.workers.dev/api/health

Expected Response:

{
  "status": "healthy",
  "environment": "production"
}

Pages Deployment

1. Build the Next.js Site

npm run cf:pages:build

This generates a static export in the out/ directory.

2. Deploy to Cloudflare Pages

npm run cf:pages:deploy

Option 2: Deploy via Cloudflare Dashboard

1. Connect GitHub Repository

  1. Log in to Cloudflare Dashboard
  2. Navigate to Pages > Create a project
  3. Connect to GitHub and select Leadership-Legacy-Startup-Agency-Website

2. Configure Build Settings

3. Set Environment Variables

In the Cloudflare Pages dashboard, add:

NEXT_PUBLIC_API_URL=https://leadership-legacy.YOUR_SUBDOMAIN.workers.dev
NODE_ENV=production

4. Deploy

Click Save and Deploy. Cloudflare will build and deploy your site.


Local Development

1. Start Next.js Development Server

npm run dev

Visit http://localhost:3000

2. Start Workers Development Server

In a separate terminal:

npm run cf:dev

This starts Wrangler’s local development server at http://localhost:8787

3. Configure Environment Variables

Create .env.local:

cp .env.example .env.local

Edit .env.local:

NEXT_PUBLIC_API_URL=http://localhost:8787
NODE_ENV=development

4. Test API Integration

# Health check
curl http://localhost:8787/api/health

# Test contact form
curl -X POST http://localhost:8787/api/contact \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Test User",
    "email": "test@example.com",
    "message": "Testing contact form"
  }'

Environment Variables

Development (.env.local)

CLOUDFLARE_ACCOUNT_ID=d3dbdd3e1ebbc28edf0ce756d9841490
CLOUDFLARE_API_TOKEN=SRt4Hz-Sk78lrbN0LMW49LkK_xGbE5CEeqT41_1e
NEXT_PUBLIC_API_URL=http://localhost:8787
NODE_ENV=development

Production (Cloudflare Pages)

Set these in the Cloudflare Pages dashboard under Settings > Environment variables:

NEXT_PUBLIC_API_URL=https://leadership-legacy.YOUR_SUBDOMAIN.workers.dev
NODE_ENV=production

Architecture Overview

┌─────────────────────────────────────────────┐
│         Cloudflare Pages (Frontend)         │
│  - Next.js 16 static export                 │
│  - Clay-inspired animations & UI            │
│  - Gold/Navy brand colors                   │
└──────────────┬──────────────────────────────┘
               │
               ├─────────────────────────────────┐
               │                                 │
┌──────────────▼──────────────┐  ┌─────────────▼──────────────┐
│   Cloudflare Workers        │  │      R2 Bucket             │
│  - Contact form API         │  │  - Images & media          │
│  - Analytics tracking       │  │  - Brand assets            │
│  - Newsletter API           │  │  - Portfolio content       │
│  - Rate limiting            │  └────────────────────────────┘
└──────────────┬──────────────┘
               │
┌──────────────▼──────────────┐
│         D1 Database         │
│  - Contact submissions      │
│  - Analytics events         │
│  - Newsletter subscribers   │
└─────────────────────────────┘

┌─────────────────────────────┐
│      Workers KV Store       │
│  - Feature flags            │
│  - Configuration            │
│  - Session data             │
└─────────────────────────────┘

Troubleshooting

Database Migration Fails

Problem: Schema migration fails with “table already exists”

Solution:

# Drop and recreate database (CAUTION: destroys data)
npx wrangler d1 delete leadership-legacy-db
npm run cf:db:create
npm run cf:db:migrate

Workers Deployment Fails

Problem: Deployment fails with authentication error

Solution:

# Re-authenticate
npx wrangler logout
npx wrangler login

API Calls Return 404

Problem: Frontend can’t reach Workers API

Solution:

  1. Verify Workers are deployed: npx wrangler deployments list
  2. Check environment variables in .env.local
  3. Ensure CORS headers are set correctly in workers/api/index.ts

Build Fails on Cloudflare Pages

Problem: Next.js build fails with module errors

Solution:

  1. Check Node version is 18+
  2. Verify all dependencies are in package.json
  3. Clear cache and retry:
    rm -rf .next node_modules package-lock.json
    npm install
    npm run build
    

Performance Targets

After deployment, verify these metrics using Lighthouse:

Key Metrics:


Next Steps

  1. Configure Custom Domain
    • Add your domain in Cloudflare Pages settings
    • Update DNS records to point to Cloudflare
  2. Set Up Analytics Dashboard
    • Query D1 database for analytics insights
    • Create visualizations in Cloudflare dashboard
  3. Enable Caching
    • Configure cache rules in Cloudflare dashboard
    • Set up page rules for optimal performance
  4. Set Up Monitoring
    • Enable Cloudflare Web Analytics
    • Set up error tracking and alerts
  5. Optimize Assets
    • Upload images to R2 bucket
    • Configure CDN caching
    • Enable Cloudflare Polish for image optimization

Support

For issues or questions:


Last Updated: December 2025 Version: 1.0.0