3.6 KiB
3.6 KiB
Cloudflare Pages Deployment Guide
This guide will help you deploy your Pilates with Fadia website to Cloudflare Pages.
Prerequisites
- A Cloudflare account
- Wrangler CLI installed:
npm install -g wrangler - Your domain (optional, Cloudflare Pages provides a free subdomain)
Setup Steps
1. Install Dependencies
# Install root dependencies
npm install
# Install client dependencies
cd client
npm install
cd ..
2. Create Cloudflare KV Namespace
# Create KV namespace for production
wrangler kv:namespace create "STORAGE"
# Create KV namespace for preview
wrangler kv:namespace create "STORAGE" --preview
Update the wrangler.toml file with the actual namespace IDs returned from the commands above.
3. Set Environment Variables
In your Cloudflare Pages dashboard, go to Settings > Environment Variables and add:
Production Environment:
JWT_SECRET: A secure random string for JWT signingMAILCHIMP_API_KEY: Your Mailchimp API keyMAILCHIMP_SERVER_PREFIX: Your Mailchimp server prefix (e.g., "us1")MAILCHIMP_LIST_ID: Your Mailchimp list ID
Preview Environment:
- Same variables as production (or use test values)
4. Build and Deploy
# Build the client
npm run build
# Deploy to Cloudflare Pages
npm run deploy
5. Configure Custom Domain (Optional)
- In Cloudflare Pages dashboard, go to your project
- Click "Custom domains"
- Add your domain
- Follow the DNS configuration instructions
Development
For local development:
# Start the development server
npm run dev
This will start the Vite development server on http://localhost:5173.
API Endpoints
The following API endpoints are available:
GET /api/classes- Get all classesGET /api/classes/:id- Get specific classPOST /api/newsletter- Subscribe to newsletterPOST /api/contact- Send contact messagePOST /api/auth/register- User registrationPOST /api/auth/login- User loginGET /api/auth/me- Get current user (requires JWT token)GET /api/bookings- Get user bookings (requires JWT token)POST /api/bookings- Create booking (requires JWT token)
Authentication
The application now uses JWT tokens instead of sessions:
- Users register/login and receive a JWT token
- The token is stored in localStorage
- All authenticated requests include the token in the Authorization header
- Tokens are verified on each request
Data Storage
Data is stored in Cloudflare KV:
- User accounts
- Newsletter subscriptions
- Contact messages
- User bookings
Troubleshooting
Build Issues
- Ensure all dependencies are installed
- Check that TypeScript compilation passes:
npm run check
API Issues
- Verify environment variables are set correctly
- Check Cloudflare Functions logs in the dashboard
- Ensure KV namespace is properly configured
Authentication Issues
- Verify JWT_SECRET is set
- Check that tokens are being sent in Authorization headers
- Ensure token format is correct (Bearer )
Security Notes
- JWT Secret: Use a strong, random secret for JWT signing
- Environment Variables: Never commit sensitive data to version control
- CORS: The middleware handles CORS for all requests
- Input Validation: All API endpoints validate input using Zod schemas
Performance
- Static assets are served from Cloudflare's global CDN
- API functions run at the edge for low latency
- KV storage provides fast data access
- Images are optimized and cached
Monitoring
- Check Cloudflare Pages analytics for traffic and performance
- Monitor function invocations and errors in the dashboard
- Set up alerts for critical errors if needed