Pre-Deployment Checklist
Before deploying to production, ensure you have:Set Up Production Database
Configure a production database (recommended: Turso). See the Database Setup guide.
Configure Environment Variables
Prepare all required environment variables for production. See the Environment Variables guide.
Environment Configuration
Required Production Variables
Set these environment variables in your deployment platform:.env.production
CORS Configuration
TheCORS_ORIGIN variable is critical for security. It controls which domains can make authenticated requests to your API.
Single Origin:
src/lib/auth.ts:
src/lib/auth.ts
For production, only include your actual production domain(s). Never use
* or include development URLs.Database Setup
Using Turso (Recommended)
Turso provides a distributed SQLite database ideal for production:Database Migrations
For production deployments, use versioned migrations:Building the Application
Build your Next.js application for production:- Compiles TypeScript code
- Optimizes bundles for production
- Generates static pages where possible
- Creates a
.nextdirectory with production artifacts
Build Script
Frompackage.json:
package.json
The build process will fail if there are TypeScript errors or build-time issues. Fix all errors before deploying.
Deployment Platforms
Vercel (Recommended)
Vercel is the easiest way to deploy Next.js applications:Configure Environment Variables
In the Vercel dashboard:
- Go to Project Settings → Environment Variables
- Add all required variables
- Set appropriate environments (Production, Preview, Development)
Vercel-Specific Tips
Automatic Deployments
Connect your GitHub repo for automatic deployments on every push.
Preview Deployments
Every PR gets a unique preview URL for testing.
Edge Network
Your app is automatically deployed to Vercel’s global edge network.
Built for Next.js
Zero-configuration deployment optimized for Next.js.
VERCEL_URL automatically. You can use it for dynamic URLs:
This automatically adapts to preview deployments, giving each PR its own working environment.
Netlify
Deploy to Netlify using the CLI or GitHub integration:Railway
Deploy to Railway for a simple, all-in-one platform:Docker
Deploy using Docker for maximum portability:Security Considerations
Secret Rotation
Rotate
BETTER_AUTH_SECRET periodically and after any suspected compromise.HTTPS Only
Always use HTTPS in production. Never deploy with
http:// URLs.Environment Isolation
Use separate databases and secrets for staging and production.
CORS Restrictions
Only include necessary origins in
CORS_ORIGIN. Never use * in production.Additional Security Best Practices
- Enable rate limiting for authentication endpoints
- Set up monitoring for suspicious activity
- Regular backups of your production database
- Keep dependencies updated for security patches
- Use environment-specific secrets (never reuse between environments)
Post-Deployment
Verify Deployment
After deploying, verify everything works:- Test authentication - Sign up and sign in
- Check database connections - Verify data reads/writes work
- Test ORPC calls - Ensure RPC endpoints respond correctly
- Monitor logs - Check for any errors
- Test CORS - Verify cross-origin requests work if applicable
Monitoring
Set up monitoring for your production application:- Error tracking: Use Sentry, LogRocket, or similar
- Performance monitoring: Monitor API response times
- Database monitoring: Track query performance and connection health
- Uptime monitoring: Use UptimeRobot, Pingdom, or similar
Database Backups
For Turso, backups are automatic. To create manual backups:Schedule regular backups and test your restore process to ensure you can recover from data loss.
Troubleshooting
Build Failures
If your build fails:- Run
pnpm buildlocally to reproduce - Check TypeScript errors:
pnpm typecheck - Verify all dependencies are installed
- Review build logs for specific errors
Environment Variable Issues
If features aren’t working:- Verify all required variables are set in your deployment platform
- Check for typos in variable names (case-sensitive)
- Ensure
NEXT_PUBLIC_prefix for client-side variables - Redeploy after adding/changing variables
Database Connection Errors
If you can’t connect to the database:- Verify
DATABASE_URLformat is correct - Check that your Turso auth token is valid
- Test connection locally with the same
DATABASE_URL - Review database logs for connection attempts
CORS Errors
If you’re getting CORS errors in production:- Verify
CORS_ORIGINexactly matches your frontend URL - Remove trailing slashes or add them consistently
- Ensure
BETTER_AUTH_URLis set correctly - Check browser console for specific CORS error messages
Continuous Deployment
Set up CI/CD for automatic deployments:GitHub Actions Example
.github/workflows/deploy.yml
Next Steps
Database Setup
Learn more about database configuration and management
Environment Variables
Detailed guide to all environment variables