Skip to content

Commit aa2106c

Browse files
authored
Merge pull request #4 from openagri-eu/dev/runtime-env-vars
Add runtime environment variable injection via `env-config.js`
2 parents 9298a67 + befec72 commit aa2106c

5 files changed

Lines changed: 31 additions & 3 deletions

File tree

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<title>OpenAgri-UserDashboard + TS</title>
88
</head>
99
<body>
10+
<script src="/env-config.js"></script>
1011
<div id="root"></div>
1112
<script type="module" src="/src/main.tsx"></script>
1213
</body>

nginx/nginx.conf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ server {
2828
add_header Access-Control-Allow-Origin *;
2929
add_header Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PATCH";
3030

31+
# This location block will serve the dynamic JavaScript file.
32+
# We are using Nginx's `return` directive to dynamically serve
33+
# the content with the environment variables replaced at runtime.
34+
location = /env-config.js {
35+
default_type application/javascript;
36+
add_header Cache-Control "no-store";
37+
return 200 'window.env = { "VITE_API_URL": "${VITE_API_URL}" };';
38+
}
39+
3140

3241
# --------------------------------------------------------------------
3342
# Core SPA Routing:

src/hooks/useFetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const useFetch = <FetchResponse = any>(
1717
const [error, setError] = useState<Error | null>(null);
1818

1919
const { session, setSession } = useSession();
20-
const apiUrl = import.meta.env.VITE_API_URL;
20+
const apiUrl = window.env?.VITE_API_URL;
2121

2222
const fetchData = async (dynamicBody = {}, dynamicHeader = {}) => {
2323
setLoading(true);

src/vite-env.d.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
interface ImportMetaEnv {
44
readonly VITE_API_URL: string;
55
}
6-
6+
77
interface ImportMeta {
88
readonly env: ImportMetaEnv;
9-
}
9+
}
10+
11+
interface Window {
12+
// This is the custom object we're adding to the window object at runtime
13+
// It contains the variables we're dynamically injecting.
14+
env: {
15+
VITE_API_URL: string;
16+
}
17+
}

vite.config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ export default defineConfig({
2525
globPatterns: ['**/*.{js,css,html,svg,png,ico}'],
2626
cleanupOutdatedCaches: true,
2727
clientsClaim: true,
28+
// also make sure it's not pre-cached
29+
globIgnores: ['**/env-config.js'],
30+
// NEVER cache the runtime env file
31+
runtimeCaching: [
32+
{
33+
urlPattern: /\/env-config\.js$/,
34+
handler: 'NetworkOnly', // or 'NetworkFirst' with low cache
35+
},
36+
],
37+
2838
},
2939

3040
devOptions: {

0 commit comments

Comments
 (0)