|
| 1 | +# Vercel Build & Memory Optimization Guide |
| 2 | + |
| 3 | +## Issues Fixed |
| 4 | + |
| 5 | +### 1. Edge Runtime Crypto Module Error ✅ |
| 6 | + |
| 7 | +**Problem**: `@privy-io/server-auth` uses Node.js `crypto` module which isn't supported in Edge Runtime |
| 8 | +**Solution**: Changed middleware to use Node.js runtime instead of Edge |
| 9 | + |
| 10 | +```typescript |
| 11 | +// packages/web/src/middleware.ts |
| 12 | +export const config = { |
| 13 | + runtime: 'nodejs', // Force Node.js runtime |
| 14 | + matcher: [...] |
| 15 | +} |
| 16 | +``` |
| 17 | + |
| 18 | +### 2. Heap Out of Memory ✅ |
| 19 | + |
| 20 | +**Problem**: Build exceeding Node.js default memory limits (8GB Vercel limit) |
| 21 | +**Solutions Applied**: |
| 22 | + |
| 23 | +- **Next.js config optimizations** (`next.config.js`): |
| 24 | + |
| 25 | + ```javascript |
| 26 | + experimental: { |
| 27 | + webpackMemoryOptimizations: true, |
| 28 | + optimizePackageImports: ['lucide-react', '@radix-ui/react-icons'], |
| 29 | + } |
| 30 | + ``` |
| 31 | + |
| 32 | +- **Build commands with increased memory**: |
| 33 | + |
| 34 | + ```json |
| 35 | + "build": "NODE_OPTIONS='--max-old-space-size=4096' pnpm next build" |
| 36 | + ``` |
| 37 | + |
| 38 | +- **Vercel.json configuration**: |
| 39 | + ```json |
| 40 | + { |
| 41 | + "buildCommand": "NODE_OPTIONS='--max-old-space-size=4096' pnpm build:vercel", |
| 42 | + "installCommand": "cd ../.. && pnpm install --filter=@zero-finance/web --frozen-lockfile --ignore-scripts --shamefully-hoist" |
| 43 | + } |
| 44 | + ``` |
| 45 | + |
| 46 | +### 3. Monorepo Configuration ✅ |
| 47 | + |
| 48 | +**Problem**: Conflicting vercel.json files (root vs packages/web) |
| 49 | +**Solution**: |
| 50 | + |
| 51 | +- ✅ Removed root `vercel.json` |
| 52 | +- ✅ Keep only `packages/web/vercel.json` with proper monorepo settings |
| 53 | + |
| 54 | +## Current Configuration |
| 55 | + |
| 56 | +### Vercel Build Settings |
| 57 | + |
| 58 | +- **Framework**: Next.js (auto-detected) |
| 59 | +- **Root Directory**: `packages/web` |
| 60 | +- **Build Command**: `NODE_OPTIONS='--max-old-space-size=4096' pnpm build:vercel` |
| 61 | +- **Install Command**: `cd ../.. && pnpm install --filter=@zero-finance/web --frozen-lockfile --ignore-scripts --shamefully-hoist` |
| 62 | +- **Memory Allocation**: 4GB for build, 8GB max on Pro tier |
| 63 | +- **Function Timeout**: 60s for API routes |
| 64 | + |
| 65 | +### Memory Limits by Tier |
| 66 | + |
| 67 | +- **Hobby**: 3GB total (2.5GB allocated to build) |
| 68 | +- **Pro**: 8GB total (7GB allocated to build) |
| 69 | +- **Enterprise**: 12GB total (10GB allocated to build) |
| 70 | + |
| 71 | +Current setting uses 4GB which works on all tiers. |
| 72 | + |
| 73 | +## Vercel Dashboard Settings |
| 74 | + |
| 75 | +Set these in your Vercel project settings: |
| 76 | + |
| 77 | +1. **Root Directory**: `packages/web` |
| 78 | +2. **Framework Preset**: Next.js |
| 79 | +3. **Build Command**: Leave as default (uses vercel.json) |
| 80 | +4. **Install Command**: Leave as default (uses vercel.json) |
| 81 | +5. **Node Version**: 22.x (matches .nvmrc) |
| 82 | + |
| 83 | +## Environment Variables Required |
| 84 | + |
| 85 | +Ensure these are set in Vercel: |
| 86 | + |
| 87 | +```bash |
| 88 | +# Privy |
| 89 | +NEXT_PUBLIC_PRIVY_APP_ID= |
| 90 | +PRIVY_APP_SECRET= |
| 91 | +PRIVY_WEBHOOK_SECRET= |
| 92 | + |
| 93 | +# Database |
| 94 | +DATABASE_URL= |
| 95 | +DIRECT_URL= |
| 96 | + |
| 97 | +# PostHog |
| 98 | +NEXT_PUBLIC_POSTHOG_KEY= |
| 99 | +NEXT_PUBLIC_POSTHOG_HOST= |
| 100 | + |
| 101 | +# Other required vars (check ENV_DEPENDENCIES_REPORT.md) |
| 102 | +``` |
| 103 | + |
| 104 | +## Build Performance Tips |
| 105 | + |
| 106 | +1. **Use Enhanced Builds (Pro/Enterprise)**: |
| 107 | + - 8 CPUs vs 4 CPUs |
| 108 | + - 16GB memory vs 8GB |
| 109 | + - 58GB disk vs 29GB |
| 110 | + - Enable in project settings |
| 111 | + |
| 112 | +2. **Optimize Dependencies**: |
| 113 | + |
| 114 | + ```bash |
| 115 | + # Check bundle size |
| 116 | + pnpm --filter @zero-finance/web build -- --experimental-debug-memory-usage |
| 117 | + ``` |
| 118 | + |
| 119 | +3. **Split Large Dependencies**: |
| 120 | + - Already configured in `next.config.js` for `circomlibjs` and `ffjavascript` |
| 121 | + |
| 122 | +4. **Monitor Build Diagnostics**: |
| 123 | + - Go to Deployment → Build Diagnostics tab |
| 124 | + - Check memory/CPU usage graphs |
| 125 | + |
| 126 | +## Troubleshooting |
| 127 | + |
| 128 | +### Build still fails with OOM |
| 129 | + |
| 130 | +1. Upgrade to Pro tier for 8GB → 16GB enhanced builds |
| 131 | +2. Increase memory: `NODE_OPTIONS='--max-old-space-size=6144'` |
| 132 | +3. Check dependency size: `pnpm list --depth=0 --json | jq '.dependencies | length'` |
| 133 | + |
| 134 | +### Typecheck fails but build succeeds |
| 135 | + |
| 136 | +- This is expected when using `ignoreDuringBuilds: true` |
| 137 | +- Run `pnpm typecheck` locally to catch issues |
| 138 | +- Fix types before deploying |
| 139 | + |
| 140 | +### Edge Runtime still causes issues |
| 141 | + |
| 142 | +- Ensure middleware.ts has `export const config = { runtime: 'nodejs' }` |
| 143 | +- API routes with Privy should NOT export `runtime = 'edge'` |
| 144 | +- Check all API routes in `app/api/` for edge runtime exports |
| 145 | + |
| 146 | +## Testing Locally |
| 147 | + |
| 148 | +Before deploying: |
| 149 | + |
| 150 | +```bash |
| 151 | +# Clean install |
| 152 | +pnpm install |
| 153 | + |
| 154 | +# Test build with memory limit |
| 155 | +NODE_OPTIONS='--max-old-space-size=4096' pnpm --filter @zero-finance/web build |
| 156 | + |
| 157 | +# Run typecheck |
| 158 | +pnpm --filter @zero-finance/web typecheck |
| 159 | + |
| 160 | +# Test locally |
| 161 | +pnpm --filter @zero-finance/web dev |
| 162 | +``` |
| 163 | + |
| 164 | +## Resources |
| 165 | + |
| 166 | +- [Vercel Memory Optimization Guide](https://vercel.com/guides/troubleshooting-sigkill-out-of-memory-errors) |
| 167 | +- [Next.js Memory Usage Docs](https://nextjs.org/docs/app/guides/memory-usage) |
| 168 | +- [Middleware Runtime Config](https://nextjs.org/docs/app/building-your-application/routing/middleware#runtime) |
0 commit comments