Dynamic Next.js Integration
This guide shows you how to integrate Sei Global Wallet with Dynamic in a Next.js application, handling both client-side and server-side rendering considerations.Prerequisites
- Node.js and npm/yarn
- Dynamic account and environment ID
- Basic understanding of Next.js and React
Step 1: Create Next.js Application
Create a new Next.js application with TypeScript:Step 2: Install Dependencies
Install the required packages:Step 3: Configure Root Layout
Update yourapp/layout.tsx
file to include the Sei Global Wallet import:
app/layout.tsx
Step 4: Create Providers Component
Create alib/providers.tsx
file to configure Dynamic and Wagmi:
lib/providers.tsx
Step 5: Create Main Page
Update yourapp/page.tsx
with the wallet connection interface:
app/page.tsx
Step 6: Environment Configuration
Create a.env.local
file for your environment variables:
.env.local
lib/providers.tsx
Step 7: Run Your Application
Start your development server:http://localhost:3000
.
Expected Behavior
Your Next.js application will:- Server-Side Rendering: Properly handle SSR without hydration errors
- Dynamic Widget: Show Dynamic’s wallet connection interface
- Sei Global Wallet: Include Sei Global Wallet in the wallet options
- Responsive Design: Work seamlessly on desktop and mobile
- Production Ready: Optimized for deployment
Next.js Specific Considerations
Client-Side Only Components
Some wallet interactions need to be client-side only:components/ClientOnlyWallet.tsx
Hydration Error Prevention
Prevent hydration errors with proper client-side checks:components/WalletStatus.tsx
Advanced Features
Custom Hook for Wallet State
Create a custom hook to manage wallet state:hooks/useWallet.ts
API Routes Integration
Create API routes that work with wallet authentication:app/api/user/route.ts
Middleware for Wallet Authentication
Create middleware to protect routes:middleware.ts
Deployment
Vercel Deployment
Deploy to Vercel with environment variables:1
Install Vercel CLI
2
Deploy to Vercel
3
Set Environment Variables
In Vercel dashboard, add your
NEXT_PUBLIC_DYNAMIC_ENVIRONMENT_ID
4
Update Dynamic Settings
Add your production domain to Dynamic’s allowed origins
Production Optimization
Optimize for production deployment:next.config.js
Troubleshooting
Hydration errors
Hydration errors
Solution: Ensure wallet components are client-side only:
Environment variables not loading
Environment variables not loading
Solution: Ensure your environment variables are prefixed with
NEXT_PUBLIC_
:Build errors with wallet imports
Build errors with wallet imports
Solution: Add webpack externals to your
next.config.js
:Next Steps
RainbowKit Integration
Compare with RainbowKit approach
Web3-React Integration
Explore Web3-React alternative