fix: production crash due to undefined import.meta.env by adding env utility and updating build script

This commit is contained in:
bipproduction
2026-02-07 17:03:42 +08:00
parent 0dd2aa17d6
commit 60457b1f2f
6 changed files with 39 additions and 17 deletions

View File

@@ -1,7 +1,8 @@
import { createAuthClient } from "better-auth/react";
import { VITE_PUBLIC_URL } from "./env";
export const authClient = createAuthClient({
baseURL: import.meta.env.VITE_PUBLIC_URL || "http://localhost:3000",
baseURL: VITE_PUBLIC_URL,
});
export const { useSession, signIn, signOut, signUp, getSession } = authClient;

23
src/utils/env.ts Normal file
View File

@@ -0,0 +1,23 @@
/**
* Safely access environment variables across different runtimes and builders.
* Supports Vite's import.meta.env and Bun's process.env (used in bun build).
*/
export const getEnv = (key: string, defaultValue = ""): string => {
// 1. Try Vite's import.meta.env
try {
if (typeof import.meta.env !== "undefined" && import.meta.env[key]) {
return import.meta.env[key];
}
} catch {}
// 2. Try process.env (injected by bun build --env)
try {
if (typeof process !== "undefined" && process.env[key]) {
return process.env[key];
}
} catch {}
return defaultValue;
};
export const VITE_PUBLIC_URL = getEnv("VITE_PUBLIC_URL", "http://localhost:3000");