Files
mypage/lib/env-validation.ts
RJ 3d79cab89a
Some checks failed
Build and Deploy Next.js Blog to Production / 🔍 Code Quality Checks (push) Failing after 18s
Build and Deploy Next.js Blog to Production / 🏗️ Build and Push Docker Image (push) Has been skipped
Build and Deploy Next.js Blog to Production / 🚀 Deploy to Production (push) Has been skipped
📄 production optimizations
2025-12-02 12:39:11 +02:00

48 lines
1.2 KiB
TypeScript

/**
* Environment variable validation for production builds
* Ensures all required environment variables are set before deployment
*/
const requiredEnvVars = [
'NEXT_PUBLIC_SITE_URL',
'NODE_ENV',
] as const
const optionalEnvVars = [
'PORT',
'HOSTNAME',
'NEXT_PUBLIC_GA_ID',
] as const
export function validateEnvironment() {
const missingVars: string[] = []
// Check required variables
for (const varName of requiredEnvVars) {
if (!process.env[varName]) {
missingVars.push(varName)
}
}
if (missingVars.length > 0) {
console.error('❌ Missing required environment variables:')
missingVars.forEach(varName => {
console.error(` - ${varName}`)
})
console.error('\n💡 Check .env.example for reference')
throw new Error('Environment validation failed')
}
// Log configuration (safe - no secrets)
if (process.env.NODE_ENV === 'production') {
console.log('✅ Environment validation passed')
console.log(` - NEXT_PUBLIC_SITE_URL: ${process.env.NEXT_PUBLIC_SITE_URL}`)
console.log(` - PORT: ${process.env.PORT || '3030'}`)
}
}
// Run validation for production builds
if (process.env.NODE_ENV === 'production') {
validateEnvironment()
}