Skip to content

Commit f5deb4f

Browse files
upgrade Privy to v3 and wagmi to latest for Next.js 15 compatibility (#286)
* upgrade Privy to v3 and wagmi to latest for Next.js 15 compatibility * build: add vercel-specific build command in web package.json
1 parent a12b3e1 commit f5deb4f

File tree

10 files changed

+360
-700
lines changed

10 files changed

+360
-700
lines changed

memory/vercel-memory-optimization.md

Lines changed: 0 additions & 58 deletions
This file was deleted.

packages/web/VERCEL_BUILD_FIX.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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)

packages/web/next.config.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
/** @type {import('next').NextConfig} */
2+
const path = require('path');
3+
24
const nextConfig = {
3-
// SEO and Performance Optimizations
45
poweredByHeader: false,
56
compress: true,
67

7-
// Image optimization for better Core Web Vitals
88
images: {
99
formats: ['image/avif', 'image/webp'],
1010
domains: ['0.finance'],
1111
minimumCacheTTL: 60,
1212
},
1313

14-
// Security headers
1514
async headers() {
1615
return [
1716
{
@@ -58,42 +57,41 @@ const nextConfig = {
5857
typescript: {
5958
tsconfigPath: './tsconfig.next.json',
6059
},
61-
// Optimize for Vercel build memory limits
6260
experimental: {
6361
webpackMemoryOptimizations: true,
62+
optimizePackageImports: ['lucide-react', '@radix-ui/react-icons'],
6463
},
65-
// Reduce build time and memory usage
66-
swcMinify: true,
64+
outputFileTracingRoot: path.join(__dirname, '../../'),
65+
staticPageGenerationTimeout: 180,
6766
webpack: (config, { webpack, isServer }) => {
68-
config.resolve.fallback = {
69-
...config.resolve.fallback,
70-
fs: false,
71-
net: false,
72-
tls: false,
73-
crypto: require.resolve('crypto-browserify'),
74-
stream: require.resolve('stream-browserify'),
75-
path: require.resolve('path-browserify'),
76-
os: require.resolve('os-browserify/browser'),
77-
};
67+
if (!isServer) {
68+
config.resolve.fallback = {
69+
...config.resolve.fallback,
70+
fs: false,
71+
net: false,
72+
tls: false,
73+
crypto: require.resolve('crypto-browserify'),
74+
stream: require.resolve('stream-browserify'),
75+
path: require.resolve('path-browserify'),
76+
os: require.resolve('os-browserify/browser'),
77+
'@react-native-async-storage/async-storage': false,
78+
};
79+
}
7880

79-
// Suppress the critical dependency warning from web-worker
8081
config.plugins.push(
8182
new webpack.ContextReplacementPlugin(/web-worker/, (data) => {
8283
delete data.dependencies[0].critical;
8384
return data;
8485
}),
8586
);
8687

87-
// Memory optimizations for Vercel
8888
if (!isServer) {
89-
// Reduce bundle size and memory usage
9089
config.optimization = {
9190
...config.optimization,
9291
splitChunks: {
9392
...config.optimization.splitChunks,
9493
cacheGroups: {
9594
...config.optimization.splitChunks.cacheGroups,
96-
// Split large dependencies into separate chunks
9795
circomlibjs: {
9896
test: /[\\/]node_modules[\\/]circomlibjs.*[\\/]/,
9997
name: 'circomlibjs',
@@ -111,17 +109,19 @@ const nextConfig = {
111109
};
112110
}
113111

114-
// Limit memory usage during webpack compilation
115112
config.optimization = {
116113
...config.optimization,
117114
minimize: process.env.NODE_ENV === 'production',
118-
// Reduce memory footprint during build
119115
moduleIds: 'deterministic',
120116
};
121117

122118
return config;
123119
},
124-
serverExternalPackages: ['require-in-the-middle'],
120+
serverExternalPackages: [
121+
'require-in-the-middle',
122+
'@metamask/sdk',
123+
'@wagmi/connectors',
124+
],
125125
};
126126

127127
module.exports = nextConfig;

packages/web/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
"version": "0.1.0",
44
"private": true,
55
"scripts": {
6-
"build": "pnpm next build ",
6+
"build": "pnpm next build",
77
"build:lite": "dotenv -e .env.lite -- next build",
88
"build:prod": "BUILD_DIR=build-prod next build",
99
"build:remote": "pnpm db:migrate && pnpm next build",
1010
"build:safe": "pnpm db:migrate:safe && pnpm next build",
11-
"build:vercel": "node scripts/build-vercel.js",
11+
"build:vercel": "pnpm build:remote",
1212
"clean": "git clean -xdf .cache .next .turbo node_modules",
1313
"dev": "pnpm dev:local",
1414
"dev:lite": "dotenv -e .env.lite -- next dev --turbo --port 3055",
@@ -47,9 +47,9 @@
4747
"@lifi/types": "^17.9.0",
4848
"@lottiefiles/react-lottie-player": "^3.6.0",
4949
"@paper-design/shaders-react": "^0.0.47",
50-
"@privy-io/react-auth": "^2.13.0",
50+
"@privy-io/react-auth": "^3.0.1",
5151
"@privy-io/server-auth": "^1.19.3",
52-
"@privy-io/wagmi": "^1.0.3",
52+
"@privy-io/wagmi": "^2.0.0",
5353
"@radix-ui/react-accordion": "^1.2.9",
5454
"@radix-ui/react-alert-dialog": "^1.1.2",
5555
"@radix-ui/react-avatar": "^1.1.10",
@@ -129,8 +129,8 @@
129129
"use-stick-to-bottom": "^1.1.1",
130130
"uuid": "^11.1.0",
131131
"vaul": "^1.1.2",
132-
"viem": "^2.23.6",
133-
"wagmi": "^2.14.6",
132+
"viem": "^2.37.12",
133+
"wagmi": "^2.17.5",
134134
"zod": "^4.1.11",
135135
"zustand": "catalog:"
136136
},

0 commit comments

Comments
 (0)