|
| 1 | +# Cloudflare Pages Deployment Guide |
| 2 | + |
| 3 | +This guide covers deploying the COC Website to Cloudflare Pages using `@cloudflare/next-on-pages`. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Node.js >= 20.0.0 (check with `node -v`) |
| 8 | +- Cloudflare account |
| 9 | +- Wrangler CLI installed (`npm install -g wrangler`) |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +### 1. Install Dependencies |
| 14 | + |
| 15 | +```bash |
| 16 | +npm install |
| 17 | +``` |
| 18 | + |
| 19 | +### 2. Configure Wrangler |
| 20 | + |
| 21 | +Login to your Cloudflare account: |
| 22 | + |
| 23 | +```bash |
| 24 | +npx wrangler login |
| 25 | +``` |
| 26 | + |
| 27 | +### 3. Add Secrets |
| 28 | + |
| 29 | +All sensitive environment variables must be added as Cloudflare secrets (not in `wrangler.toml`): |
| 30 | + |
| 31 | +```bash |
| 32 | +# Google OAuth |
| 33 | +npx wrangler secret put GOOGLE_CLIENT_ID |
| 34 | +npx wrangler secret put GOOGLE_CLIENT_SECRET |
| 35 | + |
| 36 | +# NextAuth |
| 37 | +npx wrangler secret put NEXTAUTH_SECRET |
| 38 | + |
| 39 | +# Supabase |
| 40 | +npx wrangler secret put NEXT_PUBLIC_SUPABASE_ANON_KEY |
| 41 | +npx wrangler secret put SUPABASE_SERVICE_ROLE_KEY |
| 42 | + |
| 43 | +# JWT |
| 44 | +npx wrangler secret put JWT_SECRET |
| 45 | +``` |
| 46 | + |
| 47 | +You'll be prompted to enter each value interactively. |
| 48 | + |
| 49 | +### 4. Local Development |
| 50 | + |
| 51 | +For local testing with Wrangler: |
| 52 | + |
| 53 | +1. Copy `.dev.vars.example` to `.dev.vars`: |
| 54 | + ```bash |
| 55 | + cp .dev.vars.example .dev.vars |
| 56 | + ``` |
| 57 | + |
| 58 | +2. Fill in your values in `.dev.vars` |
| 59 | + |
| 60 | +3. Run the development server: |
| 61 | + ```bash |
| 62 | + npx wrangler pages dev -- npm run dev |
| 63 | + ``` |
| 64 | + |
| 65 | +### 5. Deploy to Cloudflare Pages |
| 66 | + |
| 67 | +#### Option A: Direct Upload (Recommended for CI/CD) |
| 68 | + |
| 69 | +```bash |
| 70 | +npm run build |
| 71 | +npx @cloudflare/next-on-pages@1 |
| 72 | +npx wrangler pages deploy .vercel/output --project-name=coc-website |
| 73 | +``` |
| 74 | + |
| 75 | +#### Option B: GitHub Integration |
| 76 | + |
| 77 | +1. Go to [Cloudflare Dashboard](https://dash.cloudflare.com) |
| 78 | +2. Navigate to **Pages** → **Create a project** |
| 79 | +3. Connect your GitHub repository |
| 80 | +4. Configure build settings: |
| 81 | + - **Build command**: `npx @cloudflare/next-on-pages@1` |
| 82 | + - **Build output directory**: `.vercel/output/static` |
| 83 | + - **Root directory**: `/` |
| 84 | +5. Add all secrets in **Settings** → **Environment variables** → **Production** → **Add variable** |
| 85 | + |
| 86 | +### 6. Environment Variables in Cloudflare Dashboard |
| 87 | + |
| 88 | +For non-secret variables, add them in the Cloudflare Pages dashboard: |
| 89 | + |
| 90 | +1. Go to your project settings |
| 91 | +2. Navigate to **Environment variables** |
| 92 | +3. Add: |
| 93 | + - `NEXT_OUTPUT_STANDALONE` = `false` |
| 94 | + - `NODE_VERSION` = `20` |
| 95 | + |
| 96 | +## Troubleshooting |
| 97 | + |
| 98 | +### Build Fails with "Unsupported engine" |
| 99 | + |
| 100 | +Ensure you're using Node.js >= 20.0.0: |
| 101 | + |
| 102 | +```bash |
| 103 | +node -v # Should show v20.x.x or higher |
| 104 | +nvm use 20 # If using nvm |
| 105 | +``` |
| 106 | + |
| 107 | +### Missing Secrets |
| 108 | + |
| 109 | +If you see errors about missing environment variables at runtime: |
| 110 | + |
| 111 | +```bash |
| 112 | +# List all secrets |
| 113 | +npx wrangler secret list |
| 114 | + |
| 115 | +# Re-add any missing secrets |
| 116 | +npx wrangler secret put <SECRET_NAME> |
| 117 | +``` |
| 118 | + |
| 119 | +### Middleware Issues |
| 120 | + |
| 121 | +The middleware uses Supabase for authentication. Ensure: |
| 122 | +- `SUPABASE_SERVICE_ROLE_KEY` is correctly set as a secret |
| 123 | +- Your Supabase project is accessible from Cloudflare's edge network |
| 124 | + |
| 125 | +### Worker Size Limit Exceeded |
| 126 | + |
| 127 | +If your build exceeds Cloudflare's worker size limit: |
| 128 | + |
| 129 | +1. Check for large dependencies in your bundle |
| 130 | +2. Use dynamic imports for heavy components |
| 131 | +3. Consider moving server-heavy logic to API routes |
| 132 | + |
| 133 | +## Project Structure |
| 134 | + |
| 135 | +``` |
| 136 | +├── .dev.vars.example # Template for local development variables |
| 137 | +├── wrangler.toml # Cloudflare Pages configuration |
| 138 | +├── next.config.mjs # Next.js config (standalone output disabled for Cloudflare) |
| 139 | +└── middleware.ts # Edge middleware for auth protection |
| 140 | +``` |
| 141 | + |
| 142 | +## Configuration Files |
| 143 | + |
| 144 | +### wrangler.toml |
| 145 | + |
| 146 | +```toml |
| 147 | +name = "coc-website" |
| 148 | +compatibility_flags = ["nodejs_compat"] |
| 149 | +compatibility_date = "2025-01-01" |
| 150 | + |
| 151 | +[build] |
| 152 | +command = "npx @cloudflare/next-on-pages@1" |
| 153 | + |
| 154 | +[vars] |
| 155 | +NODE_VERSION = "20" |
| 156 | +NEXT_OUTPUT_STANDALONE = "false" |
| 157 | +``` |
| 158 | + |
| 159 | +### next.config.mjs |
| 160 | + |
| 161 | +The `output: 'standalone'` setting is disabled for Cloudflare builds. It's only enabled for Docker deployments via the `NEXT_OUTPUT_STANDALONE` environment variable. |
| 162 | + |
| 163 | +## Useful Commands |
| 164 | + |
| 165 | +```bash |
| 166 | +# Check Wrangler version |
| 167 | +wrangler --version |
| 168 | + |
| 169 | +# View deployment logs |
| 170 | +npx wrangler pages deployment list |
| 171 | + |
| 172 | +# Tail deployment logs |
| 173 | +npx wrangler tail |
| 174 | + |
| 175 | +# Open project in browser |
| 176 | +npx wrangler pages project open coc-website |
| 177 | +``` |
| 178 | + |
| 179 | +## Additional Resources |
| 180 | + |
| 181 | +- [Cloudflare Pages Documentation](https://developers.cloudflare.com/pages/) |
| 182 | +- [next-on-pages GitHub](https://github.com/cloudflare/next-on-pages) |
| 183 | +- [Wrangler CLI Docs](https://developers.cloudflare.com/workers/wrangler/) |
0 commit comments