Skip to content

The open alternative to Salesforce, designed for AI.

Remove env-config.js (#9331)

Félix Malfaite5754d215293
diff --git a/packages/twenty-front/index.html b/packages/twenty-front/index.html
index e144b28fb90..740aee9596c 100644
--- a/packages/twenty-front/index.html
+++ b/packages/twenty-front/index.html
@@ -28,7 +28,13 @@
/>
<title>Twenty</title>
- <script src="/env-config.js"></script>
+ <!-- BEGIN: Twenty Config -->
+ <script id="twenty-env-config">
+ window._env_ = {
+ // This will be overwritten
+ };
+ </script>
+ <!-- END: Twenty Config -->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script type="module">
diff --git a/packages/twenty-front/public/env-config.js b/packages/twenty-front/public/env-config.js
deleted file mode 100644
index 44364ac4ce2..00000000000
--- a/packages/twenty-front/public/env-config.js
+++ /dev/null
@@ -1,3 +0,0 @@
-window._env_ = {
- // This file should stay empty. It will be overwritten by the build process.
-};
diff --git a/packages/twenty-front/scripts/inject-runtime-env.sh b/packages/twenty-front/scripts/inject-runtime-env.sh
index 5b09e8b99fb..3c8d0521d0a 100755
--- a/packages/twenty-front/scripts/inject-runtime-env.sh
+++ b/packages/twenty-front/scripts/inject-runtime-env.sh
@@ -1,12 +1,25 @@
#!/bin/sh
-echo "Generating env-config.js file from runtime environment variables..."
-BASE_FILENAME="build/env-config.js"
-mkdir -p build
-rm -rf "./$BASE_FILENAME"
+echo "Injecting runtime environment variables into index.html..."
-{
- echo "window._env_ = {"
- echo " REACT_APP_SERVER_BASE_URL: \"$REACT_APP_SERVER_BASE_URL\","
- echo "}"
-} > "./$BASE_FILENAME"
+CONFIG_BLOCK=$(cat << EOF
+ <script id="twenty-env-config">
+ window._env_ = {
+ REACT_APP_SERVER_BASE_URL: "$REACT_APP_SERVER_BASE_URL"
+ };
+ </script>
+ <!-- END: Twenty Config -->
+EOF
+)
+# Use sed to replace the config block in index.html
+# Using pattern space to match across multiple lines
+sed -i.bak '
+ /<!-- BEGIN: Twenty Config -->/,/<!-- END: Twenty Config -->/{
+ /<!-- BEGIN: Twenty Config -->/!{
+ /<!-- END: Twenty Config -->/!d
+ }
+ /<!-- BEGIN: Twenty Config -->/r /dev/stdin
+ /<!-- END: Twenty Config -->/d
+ }
+' build/index.html <<< "$CONFIG_BLOCK"
+rm -f build/index.html.bak
diff --git a/packages/twenty-front/src/config/index.ts b/packages/twenty-front/src/config/index.ts
index 4525e814d61..a61b4ef5335 100644
--- a/packages/twenty-front/src/config/index.ts
+++ b/packages/twenty-front/src/config/index.ts
@@ -12,12 +12,12 @@ const getDefaultUrl = () => {
) {
// In development environment front and backend usually run on separate ports
// we set the default value to localhost:3000.
- // It dev context, we use env vars to overwrite it
+ // In dev context, we use env vars to overwrite it
return 'http://localhost:3000';
} else {
// Outside of localhost we assume that they run on the same port
// because the backend will serve the frontend
- // It prod context, we use env-config.js + window var to ovewrite it
+ // In prod context, we use index.html + window var to ovewrite it
return `${window.location.protocol}//${window.location.hostname}${
window.location.port ? `:${window.location.port}` : ''
}`;
diff --git a/packages/twenty-server/src/main.ts b/packages/twenty-server/src/main.ts
index 0a9e75b8d6b..54854049475 100644
--- a/packages/twenty-server/src/main.ts
+++ b/packages/twenty-server/src/main.ts
@@ -80,7 +80,7 @@ const bootstrap = async () => {
}),
);
- // Create the env-config.js of the front at runtime
+ // Inject the server url in the frontend page
generateFrontConfig();
// Enable session - Today it's used only for SSO
diff --git a/packages/twenty-server/src/utils/generate-front-config.ts b/packages/twenty-server/src/utils/generate-front-config.ts
index 5bc49063925..be82e817b43 100644
--- a/packages/twenty-server/src/utils/generate-front-config.ts
+++ b/packages/twenty-server/src/utils/generate-front-config.ts
@@ -13,23 +13,29 @@ export function generateFrontConfig(): void {
},
};
- const configString = `window._env_ = ${JSON.stringify(
- configObject.window._env_,
- null,
- 2,
- )};`;
+ const configString = `<!-- BEGIN: Twenty Config -->
+ <script id="twenty-env-config">
+ window._env_ = ${JSON.stringify(configObject.window._env_, null, 2)};
+ </script>
+ <!-- END: Twenty Config -->`;
const distPath = path.join(__dirname, '../..', 'front');
- const filePath = path.join(distPath, 'env-config.js');
+ const indexPath = path.join(distPath, 'index.html');
- if (!fs.existsSync(distPath)) {
- fs.mkdirSync(distPath, { recursive: true });
- }
+ if (!fs.existsSync(indexPath)) {
+ console.log(
+ 'Frontend build not found, assuming it is served independently',
+ );
- if (
- !fs.existsSync(filePath) ||
- fs.readFileSync(filePath, 'utf8') !== configString
- ) {
- fs.writeFileSync(filePath, configString, 'utf8');
+ return;
}
+
+ let indexContent = fs.readFileSync(indexPath, 'utf8');
+
+ indexContent = indexContent.replace(
+ /<!-- BEGIN: Twenty Config -->[\s\S]*?<!-- END: Twenty Config -->/,
+ configString,
+ );
+
+ fs.writeFileSync(indexPath, indexContent, 'utf8');
}
diff --git a/packages/twenty-ui/src/utilities/config/index.ts b/packages/twenty-ui/src/utilities/config/index.ts
index 5038c501089..5a9b2ce3986 100644
--- a/packages/twenty-ui/src/utilities/config/index.ts
+++ b/packages/twenty-ui/src/utilities/config/index.ts
@@ -12,12 +12,12 @@ const getDefaultUrl = () => {
) {
// In development environment front and backend usually run on separate ports
// we set the default value to localhost:3000.
- // It dev context, we use env vars to overwrite it
+ // In dev context, we use env vars to overwrite it
return 'http://localhost:3000';
} else {
// Outside of localhost we assume that they run on the same port
// because the backend will serve the frontend
- // It prod context, we use env-config.js + window var to ovewrite it
+ // In prod context, we use index.html + window var to ovewrite it
return `${window.location.protocol}//${window.location.hostname}${
window.location.port ? `:${window.location.port}` : ''
}`;