Build for production
Coffee Finder uses Next.js standalone output mode for optimized production builds.Build the application
Run the production build command:This command performs the following:
- Runs
next buildto create an optimized production bundle - Copies static assets to the standalone directory
- Copies public files to the standalone directory
package.json:package.json
Standalone mode bundles all dependencies into a single server, reducing deployment size and complexity.
Verify build output
After building, check the output structure:You should see:
server.js- Production server entry point.next/- Build artifacts and static filespublic/- Public assetspackage.json- Minimal production dependencies
Test production build locally
Start the production server:This runs
next start -p 3000. Visit http://localhost:3000 to verify the build.Database preparation
Before deploying, ensure your database is ready for production.Run database migrations
Apply all database migrations:
For production databases, use migrations instead of
db:push to maintain a migration history.Caddy reverse proxy
Coffee Finder includes aCaddyfile for production deployment with automatic HTTPS.
Caddyfile configuration
The includedCaddyfile configures Caddy to reverse proxy to the Next.js application:
Caddyfile
Understanding the Caddyfile
:81 directive
:81 directive
Caddy listens on port 81 instead of the standard port 80. This is useful for:Caddy automatically provisions SSL certificates from Let’s Encrypt.
- Running behind another reverse proxy
- Containerized deployments
- Development and staging environments
Caddyfile
@transform_port_query matcher
@transform_port_query matcher
This matcher allows dynamic port routing based on query parameters:Useful for multi-service deployments or A/B testing.
Header forwarding
Header forwarding
The configuration forwards important headers:
Host- Original host headerX-Forwarded-For- Client IP addressX-Forwarded-Proto- Original protocol (http/https)X-Real-IP- Real client IP
Running with Caddy
Docker deployment
Deploy Coffee Finder using Docker for consistent, portable deployments.Create Dockerfile
Create aDockerfile in the project root:
Dockerfile
Build Docker image
Run Docker container
Docker Compose
Use Docker Compose for multi-container deployments:docker-compose.yml
Platform deployments
Deploy Coffee Finder to popular hosting platforms.Vercel
Vercel provides the simplest deployment for Next.js applications.Vercel automatically handles builds, caching, and CDN distribution. No Dockerfile or Caddy configuration needed.
Railway
Deploy to Railway with automatic PostgreSQL provisioning:DigitalOcean App Platform
Environment variables checklist
Before deploying, ensure all required environment variables are set:-
DATABASE_URL- Production database connection string -
NODE_ENV=production- Set environment to production -
NEXT_PUBLIC_APP_URL- Public application URL -
OVERPASS_API_URL- (Optional) Custom Overpass API endpoint
Post-deployment
After deploying:Performance optimization
Enable compression
Enable compression
Caddy automatically enables compression. For other reverse proxies, ensure gzip or brotli is enabled.
Configure caching
Configure caching
Add caching headers in
next.config.ts:Use a CDN
Use a CDN
Serve static assets from a CDN:
- Vercel automatically provides CDN
- Self-hosted: Use Cloudflare, Fastly, or AWS CloudFront
Troubleshooting
Build fails with memory error
Build fails with memory error
Increase Node.js memory limit:
package.json
Database connection errors
Database connection errors
Verify:
DATABASE_URLis correct- Database server is accessible from application server
- Firewall allows connections
- SSL mode matches database configuration
404 errors on refresh
404 errors on refresh
Ensure your reverse proxy routes all requests to Next.js:
Caddyfile
Next steps
Your application is now deployed!- Customize the theme - Brand your application
- API reference - Explore the Coffee Finder API