Skip to content

Commit d462c87

Browse files
committed
⚡ enhance frontend vars handling
1 parent 81f3ef7 commit d462c87

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

web/start/.env.development

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
VITE_APP_LOGO_URL=""
2+
VITE_API_URL=""
3+
VITE_APP_TITLE=""

web/start/src/App.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import {ToastProvider} from "@/components/ui/toast.tsx";
77
import {Toaster} from "@/components/ui/toaster.tsx";
88
import Logo from './assets/logo.png';
99
import {Separator} from "@/components/ui/separator.tsx";
10+
import getConfig from "@/config.ts";
1011

1112
const App: React.FC = () => {
1213
const [blueprints, setBlueprints] = useState<Blueprint[]>([]);
1314
useEffect(() => {
14-
document.title = window.VITE_APP_TITLE || 'Power - Starters';
15+
document.title = getConfig('VITE_APP_TITLE') || 'Power - Starters';
1516
fetchBlueprints().then(setBlueprints);
1617
}, []);
1718

@@ -35,7 +36,7 @@ const App: React.FC = () => {
3536
}
3637

3738
const getLogo = () => {
38-
const logoUrl = window.VITE_APP_LOGO_URL;
39+
const logoUrl = getConfig('VITE_APP_LOGO_URL')
3940
return (
4041
<img src={logoUrl || Logo} alt="logo" className="h-12" />
4142
);

web/start/src/api.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import axios, {AxiosError, AxiosResponse} from 'axios';
2+
import getConfig from "@/config.ts";
23

34
export const API_BASE_URL =
4-
window.VITE_API_URL || 'http://localhost:3000';
5+
getConfig('window.VITE_API_URL') || 'http://localhost:8000';
56

67
interface Blueprint {
78
type: string;

web/start/src/config.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// src/utils/config.js
2+
function getConfig(key: string) {
3+
// Check if the app is running in production
4+
if (import.meta.env.PROD) {
5+
// Production environment: Read from global window object
6+
return window[key];
7+
} else {
8+
// Development environment: Use Vite's import.meta.env
9+
return import.meta.env[key];
10+
}
11+
}
12+
13+
export default getConfig;

web/start/src/globals.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ declare global {
44
VITE_APP_LOGO_URL?: string;
55
VITE_API_URL?: string;
66
VITE_APP_TITLE?: string;
7+
[key: string]: any;
78
}
89
}
910

0 commit comments

Comments
 (0)