Skip to content

Commit

Permalink
Merge pull request #387 from captableinc/fix-runtime-env
Browse files Browse the repository at this point in the history
fix: runtime env error message
  • Loading branch information
dahal authored Jun 13, 2024
2 parents c4451f8 + 5f11e1b commit b484bb5
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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");
}
},
});

0 comments on commit b484bb5

Please sign in to comment.