This guide provides step-by-step instructions for deploying the Posyandu Lansia frontend application to various hosting platforms.
- Prerequisites
- Environment Variables
- Local Production Build
- Deployment Platforms
- Post-Deployment
- Troubleshooting
Before deploying, ensure you have:
-
Backend API deployed and accessible
- Backend URL (e.g.,
https://api.posyandu-lansia.com) - CORS configured to allow your frontend domain
- Backend URL (e.g.,
-
Node.js and npm installed (for local testing)
- Node.js 18.x or higher
- npm 9.x or higher
-
Git repository (for platform deployments)
- Code pushed to GitHub, GitLab, or Bitbucket
-
Environment variables ready
- Backend API URL
- Any additional configuration values
| Variable | Description | Example |
|---|---|---|
NEXT_PUBLIC_API_URL |
Backend API base URL | https://api.posyandu-lansia.com |
| Variable | Description | Default |
|---|---|---|
NEXT_PUBLIC_APP_NAME |
Application name | Sistem Rekam Medis Digital Posyandu Lansia |
NEXT_PUBLIC_APP_VERSION |
Application version | 1.0.0 |
NEXT_PUBLIC_DEBUG |
Enable debug mode | false |
NEXT_PUBLIC_SESSION_TIMEOUT |
Session timeout (ms) | 86400000 (24 hours) |
# Copy example file
cp .env.example .env.local
# Edit .env.local with your values
# Windows: notepad .env.local
# Mac/Linux: nano .env.localSet environment variables in your hosting platform's dashboard (see platform-specific instructions below).
Test your production build locally before deploying:
npm installnpm run type-checknpm run lintnpm run buildnpm run startOpen http://localhost:3000 in your browser and test all features.
npm run build:productionThis command runs type-check, lint, and build in sequence.
Vercel is the recommended platform for Next.js applications with zero-configuration deployment.
- Vercel account (free tier available)
- Git repository connected to Vercel
-
Install Vercel CLI (optional)
npm install -g vercel
-
Deploy via Dashboard
- Go to vercel.com
- Click "New Project"
- Import your Git repository
- Configure project:
- Framework Preset: Next.js
- Root Directory:
fe(if monorepo) - Build Command:
npm run build - Output Directory:
.next
- Add environment variables:
NEXT_PUBLIC_API_URL: Your backend URL
- Click "Deploy"
-
Deploy via CLI
cd fe vercelFollow the prompts to configure and deploy.
-
Configure Custom Domain (optional)
- Go to Project Settings > Domains
- Add your custom domain
- Update DNS records as instructed
- Go to Project Settings > Environment Variables
- Add each variable:
- Name:
NEXT_PUBLIC_API_URL - Value:
https://your-backend.vercel.app - Environment: Production, Preview, Development
- Name:
- Redeploy to apply changes
Vercel automatically deploys:
- Production: When you push to
mainbranch - Preview: When you create a pull request
Netlify is another excellent platform for Next.js applications.
-
Connect Repository
- Go to netlify.com
- Click "New site from Git"
- Choose your Git provider and repository
-
Configure Build Settings
- Base directory:
fe(if monorepo) - Build command:
npm run build - Publish directory:
.next
- Base directory:
-
Add Environment Variables
- Go to Site settings > Build & deploy > Environment
- Add variables:
NEXT_PUBLIC_API_URL: Your backend URL
-
Deploy
- Click "Deploy site"
- Wait for build to complete
Create fe/netlify.toml:
[build]
command = "npm run build"
publish = ".next"
[build.environment]
NODE_VERSION = "18"
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
[[headers]]
for = "/*"
[headers.values]
X-Frame-Options = "SAMEORIGIN"
X-Content-Type-Options = "nosniff"
X-XSS-Protection = "1; mode=block"Deploy using Docker for more control over the environment.
Create fe/Dockerfile:
# Stage 1: Dependencies
FROM node:18-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
# Stage 2: Builder
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Stage 3: Runner
FROM node:18-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT 3000
CMD ["node", "server.js"]Create fe/docker-compose.yml:
version: '3.8'
services:
frontend:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"
environment:
- NEXT_PUBLIC_API_URL=https://your-backend.com
restart: unless-stopped# Build image
docker build -t posyandu-lansia-frontend .
# Run container
docker run -p 3000:3000 \
-e NEXT_PUBLIC_API_URL=https://your-backend.com \
posyandu-lansia-frontend
# Or use docker-compose
docker-compose up -d- Application loads successfully
- Login functionality works
- API calls to backend succeed
- All pages are accessible
- QR code scanning works (if using HTTPS)
- Images load correctly
- No console errors
Ensure your backend allows requests from your frontend domain:
// Backend CORS configuration example
const corsOptions = {
origin: [
'http://localhost:3000',
'https://your-frontend.vercel.app',
'https://your-custom-domain.com'
],
credentials: true
};- Go to Project Settings > Domains
- Add your domain
- Update DNS records:
- Type:
AorCNAME - Name:
@orwww - Value: Provided by Vercel
- Type:
- Go to Domain settings
- Add custom domain
- Update DNS records as instructed
Both Vercel and Netlify automatically provide SSL certificates via Let's Encrypt.
For custom domains:
- Vercel: Automatic SSL
- Netlify: Automatic SSL
- Docker: Use reverse proxy (Nginx, Caddy) with Let's Encrypt
- Enable in Project Settings > Analytics
- Free tier includes basic metrics
Add to app/layout.tsx:
import Script from 'next/script';
// Add in <head>
<Script
src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"
strategy="afterInteractive"
/>
<Script id="google-analytics" strategy="afterInteractive">
{`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA_MEASUREMENT_ID');
`}
</Script>- Enable caching headers
- Optimize images
- Enable compression
- Use CDN for static assets
- Monitor Core Web Vitals
Problem: Build fails with TypeScript errors
Solution:
# Run type check locally
npm run type-check
# Fix errors and commit
git add .
git commit -m "fix: resolve TypeScript errors"
git pushProblem: Build fails with ESLint errors
Solution:
# Run linter locally
npm run lint
# Auto-fix issues
npm run lint:fix
# Commit fixes
git add .
git commit -m "fix: resolve linting errors"
git pushProblem: Frontend cannot connect to backend
Solution:
- Verify
NEXT_PUBLIC_API_URLis set correctly - Check backend CORS configuration
- Ensure backend is accessible from frontend domain
- Check browser console for CORS errors
Problem: Environment variables are undefined
Solution:
- Ensure variables are prefixed with
NEXT_PUBLIC_ - Redeploy after adding variables
- Clear build cache and rebuild
- Check variable names for typos
Problem: QR code images or other images fail to load
Solution:
- Check
next.config.tsimage domains configuration - Add backend domain to
remotePatterns - Verify image URLs are correct
- Check CORS headers on image requests
Problem: Camera access denied or scanner fails
Solution:
- Ensure site is served over HTTPS (required for camera access)
- Check browser permissions for camera
- Test on different browsers
- Verify
html5-qrcodelibrary is loaded
Problem: Application loads slowly
Solution:
- Run Lighthouse audit
- Optimize images (use WebP format)
- Enable code splitting
- Use dynamic imports for heavy components
- Enable caching headers
- Use CDN for static assets
Problem: Users are logged out frequently
Solution:
- Increase
NEXT_PUBLIC_SESSION_TIMEOUTvalue - Implement token refresh mechanism
- Check backend JWT expiration settings
# Update dependencies
npm update
# Check for security vulnerabilities
npm audit
# Fix vulnerabilities
npm audit fix- Monitor error logs in hosting platform
- Track performance metrics
- Review user feedback
- Monitor API response times
- Keep Git repository up to date
- Document configuration changes
- Maintain environment variable documentation
For issues or questions:
- Check Next.js Documentation
- Review Vercel Documentation
- Contact your development team
Before going live:
- All tests pass
- Type checking passes
- Linting passes
- Production build succeeds
- Environment variables configured
- Backend CORS configured
- Custom domain configured (if applicable)
- HTTPS enabled
- Monitoring setup
- Error tracking configured
- Performance optimized
- Documentation updated
- Team trained on deployment process
Last Updated: 2024 Version: 1.0.0