fix: exhaustive API key lookup and injection for Vercel

This commit is contained in:
Christian Vidal Wolf
2026-02-10 15:24:52 +01:00
parent 01026da882
commit a259538ff4
2 changed files with 14 additions and 5 deletions
+10 -3
View File
@@ -29,11 +29,18 @@ Guidelines:
// Helper to get API key safely // Helper to get API key safely
const getApiKey = (): string | undefined => { const getApiKey = (): string | undefined => {
try { // Try different sources (Vite defines these at build time)
return process.env.API_KEY; const key =
} catch (e) { (typeof process !== 'undefined' && process.env ? process.env.API_KEY || process.env.GEMINI_API_KEY : undefined) ||
(import.meta as any).env?.VITE_GEMINI_API_KEY ||
(import.meta as any).env?.API_KEY ||
(import.meta as any).env?.GEMINI_API_KEY;
// Ensure it's not a literal "undefined" string from a failed build injection
if (key === "undefined" || key === "null" || !key) {
return undefined; return undefined;
} }
return key;
}; };
const formatCurrency = (val: number) => `${val.toLocaleString('de-DE', { minimumFractionDigits: 0, maximumFractionDigits: 0 })}`; const formatCurrency = (val: number) => `${val.toLocaleString('de-DE', { minimumFractionDigits: 0, maximumFractionDigits: 0 })}`;
+4 -2
View File
@@ -49,8 +49,10 @@ export default defineConfig(({ mode }) => {
}, },
plugins: [react()], plugins: [react()],
define: { define: {
'process.env.API_KEY': JSON.stringify(env.GEMINI_API_KEY || env.API_KEY), 'process.env.API_KEY': JSON.stringify(env.GEMINI_API_KEY || env.API_KEY || ""),
'process.env.GEMINI_API_KEY': JSON.stringify(env.GEMINI_API_KEY || env.API_KEY) 'process.env.GEMINI_API_KEY': JSON.stringify(env.GEMINI_API_KEY || env.API_KEY || ""),
'import.meta.env.VITE_GEMINI_API_KEY': JSON.stringify(env.GEMINI_API_KEY || env.API_KEY || ""),
'import.meta.env.API_KEY': JSON.stringify(env.GEMINI_API_KEY || env.API_KEY || "")
}, },
resolve: { resolve: {
alias: { alias: {