From 5f11e1ba5dfc7aa9f0421b70a962e8581f8fe9e9 Mon Sep 17 00:00:00 2001 From: nafees nazik Date: Thu, 13 Jun 2024 00:40:41 +0530 Subject: [PATCH] fix: env --- src/env.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/env.js b/src/env.js index a7dfd7e6a..b583f4401 100644 --- a/src/env.js +++ b/src/env.js @@ -2,9 +2,18 @@ import { createEnv } from "@t3-oss/env-nextjs"; import { z } from "zod"; +const PUBLIC_ENV_KEY = "___ENV"; + +function isBrowser() { + return typeof window !== "undefined"; +} + const readRuntimePublicEnvVariable = (key) => { - if (typeof window === "undefined") return process.env[key]; - return window.___ENV[key]; + if (isBrowser()) { + return window?.[PUBLIC_ENV_KEY]?.[key]; + } + + return process.env[key]; }; export const env = createEnv({ @@ -60,7 +69,7 @@ export const env = createEnv({ * You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g. * middlewares) or client-side so we need to destruct manually. */ - runtimeEnv: { + experimental__runtimeEnv: { NODE_ENV: process.env.NODE_ENV, NEXT_PUBLIC_BASE_URL: readRuntimePublicEnvVariable("NEXT_PUBLIC_BASE_URL"), DATABASE_URL: process.env.DATABASE_URL, @@ -98,4 +107,14 @@ export const env = createEnv({ * `SOME_VAR=''` will throw an error. */ emptyStringAsUndefined: true, + + onValidationError: (error) => { + if (!isBrowser()) { + console.error( + "❌ Invalid environment variables:", + error.flatten().fieldErrors, + ); + throw new Error("Invalid environment variables"); + } + }, });