Deployment
Learn how to deploy your Metronic Next.js application to various hosting platforms.
Deployment
This guide covers deploying your Metronic Next.js application to production environments.
Building for Production
# Navigate to your project directory
cd your-nextjs-project
# Install dependencies (if needed)
npm install
# Build for production
npm run buildThis creates a .next directory with optimized files ready for deployment.
Configuration
Environment Variables
For production, create a .env.production file:
NEXT_PUBLIC_APP_NAME=metronic-nextjs
NEXT_PUBLIC_API_URL=https://your-production-api.com/api
NEXTAUTH_URL=https://your-domain.com
NEXTAUTH_SECRET=your-secret-key
Remember that only variables prefixed with NEXT_PUBLIC_ will be exposed to the browser.
Base Path Configuration
When deploying your Next.js application behind a reverse proxy (like nginx) or to a subdirectory, you need to configure the basePath in your next.config.mjs file.
Understanding Base Path
The basePath tells Next.js that your application is served from a subdirectory rather than the root domain. For example:
- Root deployment:
https://example.com/→basePath: ''orbasePath: '/' - Subdirectory deployment:
https://example.com/my-app/→basePath: '/my-app'
Configuration Options
Option 1: Using Environment Variable (Recommended)
Set the NEXT_PUBLIC_BASE_PATH environment variable in your .env.production file:
# For subdirectory deployment
NEXT_PUBLIC_BASE_PATH=/Option 2: Hardcoded in Config
You can also hardcode the basePath directly in next.config.mjs:
const nextConfig = {
basePath: '/your-custom-path',
assetPrefix: '/your-custom-path',
output: 'standalone',
};Important Notes
-
Path Format: The
basePathmust be a path string (e.g.,/my-app), not a full URL. If your environment variable contains a full URL, you'll need to extract just the pathname. -
Trailing Slash: Next.js handles trailing slashes automatically, so both
/my-appand/my-app/work correctly. -
Asset Prefix: The
assetPrefixshould match yourbasePathto ensure static assets (CSS, JS, images) are loaded from the correct path. -
Links and Navigation: When using
basePath, Next.js automatically prefixes all internal links and routes. However, if you're using thetoAbsoluteUrlhelper function fromlib/helpers.ts, it usesNEXT_PUBLIC_BASE_PATHfor absolute URLs. -
Development vs Production: You can use different base paths for development and production by setting different values in
.envand.env.productionfiles.
Example: Deploying Behind Nginx
If you're deploying behind an nginx reverse proxy:
location /my-app {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}Then set in your .env.production:
NEXT_PUBLIC_BASE_PATH=/my-appTroubleshooting Base Path Issues
If you're experiencing issues with assets not loading or routes not working:
-
Check Environment Variable: Ensure
NEXT_PUBLIC_BASE_PATHis set correctly in your production environment. -
Rebuild After Changes: After changing
basePath, you must rebuild your application:npm run build -
Verify Asset Loading: Check your browser's Network tab to see if assets are being requested from the correct path.
-
Check Router Configuration: Ensure your reverse proxy (nginx, Apache, etc.) is configured to pass requests to the correct path.
Deployment Options
Vercel (Recommended)
Vercel provides the simplest deployment experience for Next.js applications:
- Push your code to a Git repository (GitHub, GitLab, BitBucket)
- Import your repository on vercel.com
- Vercel will automatically detect Next.js and use the optimal build settings
Alternatively, use the Vercel CLI:
# Install Vercel CLI
npm install -g vercel
# Deploy
vercelNext.js-Specific Deployment Options
Self-Hosted Static Export
For static site hosting (without server-side features):
- Add this to your
next.config.mjs:
export default {
output: 'export',
}- Build your application:
npm run buildThis generates a static out directory you can deploy to any static hosting service.
Node.js Server
For server-side rendering with a Node.js server:
- Build your application:
npm run build- Start the Node.js server:
npm run startThis starts a production server on port 3000 by default.
Common Issues
Image Optimization
When using Next.js Image Optimization with static export, configure the loader in next.config.mjs:
export default {
images: {
loader: 'custom',
loaderFile: './my-image-loader.js',
}
}Create a custom loader file:
// my-image-loader.js
export default function myImageLoader({ src, width, quality }) {
return `https://your-cdn.com/${src}?w=${width}&q=${quality || 75}`
}API Routes
API routes don't work with static exports. You'll need a server-side deployment method or serverless functions if you're using API routes.
Server Components
Server components require a server-side deployment method, such as Vercel, Node.js server, or Docker. They won't work with static exports.
Testing Production Build Locally
To preview your production build before deployment:
npm run build
npm run startVisit http://localhost:3000 to preview your production build.
Continuous Deployment
For automated deployments, configure CI/CD pipelines:
GitHub Actions Example
Create a .github/workflows/deploy.yml file:
name: Deploy Next.js site to Vercel
on:
push:
branches: ["main"]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Install Vercel CLI
run: npm install --global vercel@latest
- name: Deploy to Vercel
run: vercel deploy --prod --token=${{ secrets.VERCEL_TOKEN }}For more information on Next.js deployment, refer to the official documentation.