env
All checks were successful
Build & Deploy KLZ Cables / build-and-deploy (push) Successful in 4m12s

This commit is contained in:
2026-01-28 15:14:01 +01:00
parent d6c1d6bae6
commit 91ebc54571
2 changed files with 11 additions and 20 deletions

View File

@@ -1,9 +1,7 @@
import './load-env';
import { z } from 'zod'; import { z } from 'zod';
/** /**
* Environment variable schema. * Environment variable schema.
* This ensures the application has all required configuration at startup.
*/ */
const envSchema = z.object({ const envSchema = z.object({
NODE_ENV: z.enum(['development', 'production', 'test']).default('development'), NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
@@ -32,17 +30,14 @@ const envSchema = z.object({
MAIL_RECIPIENTS: z.string().optional().transform(val => val?.split(',').filter(Boolean) || []), MAIL_RECIPIENTS: z.string().optional().transform(val => val?.split(',').filter(Boolean) || []),
}); });
export type Env = z.infer<typeof envSchema>;
/** /**
* Helper to get environment variables. * Helper to get environment variables.
* On the client, only NEXT_PUBLIC_ variables are available.
* On the server, all variables are available.
*/ */
function getRawEnv() { function getRawEnv() {
if (typeof window !== 'undefined') { const isServer = typeof window === 'undefined';
if (!isServer) {
// Client-side: only return NEXT_PUBLIC_ variables // Client-side: only return NEXT_PUBLIC_ variables
// We must explicitly reference them for Next.js inlining to work
return { return {
NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL, NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL,
NEXT_PUBLIC_UMAMI_WEBSITE_ID: process.env.NEXT_PUBLIC_UMAMI_WEBSITE_ID, NEXT_PUBLIC_UMAMI_WEBSITE_ID: process.env.NEXT_PUBLIC_UMAMI_WEBSITE_ID,
@@ -50,7 +45,7 @@ function getRawEnv() {
}; };
} }
// Server-side: return all variables from process.env // Server-side: return all variables
return { return {
NODE_ENV: process.env.NODE_ENV, NODE_ENV: process.env.NODE_ENV,
NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL, NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL,
@@ -71,5 +66,11 @@ function getRawEnv() {
/** /**
* Validated environment variables. * Validated environment variables.
* We use safeParse during build to avoid crashing the build process.
*/ */
export const env = envSchema.parse(getRawEnv()); const isBuildTime = process.env.NEXT_PHASE === 'phase-production-build';
const parsed = isBuildTime
? envSchema.safeParse(getRawEnv())
: { success: true, data: envSchema.parse(getRawEnv()) };
export const env = parsed.success ? parsed.data : envSchema.parse({});

View File

@@ -1,10 +0,0 @@
import dotenv from 'dotenv';
import path from 'path';
/**
* Loads environment variables from .env file in development.
* This must be called before any other module that uses environment variables.
*/
if (typeof window === 'undefined' && process.env.NODE_ENV !== 'production') {
dotenv.config({ path: path.resolve(process.cwd(), '.env') });
}