Skip to content

Commit d5412f9

Browse files
committed
feat: updated frontend use run time config inject value
1 parent d0648ca commit d5412f9

File tree

17 files changed

+111
-41
lines changed

17 files changed

+111
-41
lines changed

frontend/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"
2121
/>
2222
<title>Infisical</title>
23+
<script src="/runtime-ui-env.js"></script>
2324
</head>
2425
<body>
2526
<div id="root"></div>

frontend/scripts/initialize-standalone-build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ scripts/replace-standalone-build-variable.sh "$BAKED_NEXT_PUBLIC_INTERCOM_ID" "$
66

77
scripts/replace-standalone-build-variable.sh "$BAKED_NEXT_PUBLIC_CAPTCHA_SITE_KEY" "$NEXT_PUBLIC_CAPTCHA_SITE_KEY"
88

9+
scripts/set-frontend-config.sh
10+
911
if [ "$TELEMETRY_ENABLED" != "false" ]; then
1012
echo "Telemetry is enabled"
1113
scripts/set-standalone-build-telemetry.sh true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
# Configuration output file
4+
CONFIG_FILE="runtime-config.js"
5+
6+
# Replace content in the config file with SENTRY_DSN interpolation
7+
echo "window.__CONFIG__ = Object.freeze({ CAPTCHA_SITE_KEY: \"${CAPTCHA_SITE_KEY}\", CAPTCHA_SITE_KEY: \"${CAPTCHA_SITE_KEY}\", CAPTCHA_SITE_KEY: \"${CAPTCHA_SITE_KEY}\" })" > $CONFIG_FILE
8+
9+
echo "Configuration file updated at $CONFIG_FILE"

frontend/src/components/analytics/posthog.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
/* eslint-disable @typescript-eslint/ban-ts-comment */
2-
/* eslint-disable no-undef */
1+
import { envConfig } from "@app/config/env";
32
import posthog from "posthog-js";
43

5-
import { ENV, POSTHOG_API_KEY, POSTHOG_HOST } from "../utilities/config";
6-
74
export const initPostHog = () => {
8-
// @ts-ignore
95
console.log("Hi there 👋");
106
try {
117
if (typeof window !== "undefined") {
12-
// @ts-ignore
13-
if (ENV === "production" && TELEMETRY_CAPTURING_ENABLED === true) {
14-
posthog.init(POSTHOG_API_KEY, {
15-
api_host: POSTHOG_HOST
8+
if (
9+
envConfig.ENV === "production" &&
10+
envConfig.TELEMETRY_CAPTURING_ENABLED === true &&
11+
envConfig.POSTHOG_API_KEY
12+
) {
13+
posthog.init(envConfig.POSTHOG_API_KEY, {
14+
api_host: envConfig.POSTHOG_HOST
1615
});
1716
}
1817
}

frontend/src/components/navigation/RegionSelect.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export const RegionSelect = () => {
107107
))}
108108
</Select>
109109
<Modal>
110-
<ModalTrigger>
110+
<ModalTrigger asChild>
111111
<button type="button" className="mt-1 text-right text-xs text-mineshaft-400 underline">
112112
Help me pick a data region
113113
</button>

frontend/src/components/utilities/config/index.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

frontend/src/components/utilities/telemetry/Telemetry.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/* eslint-disable */
22
import { PostHog } from "posthog-js";
33
import { initPostHog } from "@app/components/analytics/posthog";
4-
import { ENV } from "@app/components/utilities/config";
5-
6-
declare let TELEMETRY_CAPTURING_ENABLED: any;
4+
import { envConfig } from "@app/config/env";
75

86
class Capturer {
97
api: PostHog;
@@ -13,7 +11,7 @@ class Capturer {
1311
}
1412

1513
capture(item: string) {
16-
if (ENV === "production" && TELEMETRY_CAPTURING_ENABLED === true) {
14+
if (envConfig.ENV === "production" && envConfig.TELEMETRY_CAPTURING_ENABLED === true) {
1715
try {
1816
this.api.capture(item);
1917
} catch (error) {
@@ -23,7 +21,7 @@ class Capturer {
2321
}
2422

2523
identify(id: string, email?: string) {
26-
if (ENV === "production" && TELEMETRY_CAPTURING_ENABLED === true) {
24+
if (envConfig.ENV === "production" && envConfig.TELEMETRY_CAPTURING_ENABLED === true) {
2725
try {
2826
this.api.identify(id, {
2927
email: email

frontend/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(window as any).__config__ = {};

frontend/src/config/env.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// akhilmhdh: These are runtime environment variables loaded from server
2+
// The window body is filled with value from the server
3+
// We add a script in index.html to load it from server before react loads
4+
/* eslint-disable no-underscore-dangle */
5+
export const envConfig = {
6+
ENV: import.meta.env.MODE,
7+
get CAPTCHA_SITE_KEY() {
8+
return (
9+
window?.__INFISICAL_RUNTIME_ENV__?.CAPTCHA_SITE_KEY || import.meta.env.VITE_CAPTCHA_SITE_KEY
10+
);
11+
},
12+
get INTERCOM_ID() {
13+
return window?.__INFISICAL_RUNTIME_ENV__?.INTERCOM_ID || import.meta.env.VITE_INTERCOM_ID;
14+
},
15+
get POSTHOG_API_KEY() {
16+
return (
17+
window?.__INFISICAL_RUNTIME_ENV__?.POSTHOG_API_KEY || import.meta.env.VITE_POSTHOG_API_KEY
18+
);
19+
},
20+
get POSTHOG_HOST() {
21+
return import.meta.env.VITE_POSTHOG_HOST! || "https://app.posthog.com";
22+
},
23+
get TELEMETRY_CAPTURING_ENABLED() {
24+
return (
25+
window?.__INFISICAL_RUNTIME_ENV__?.TELEMETRY_CAPTURING_ENABLED ||
26+
import.meta.env.VITE_TELEMETRY_CAPTURING_ENABLED === true
27+
);
28+
},
29+
get PLATFORM_VERSION() {
30+
return import.meta.env.VITE_INFISICAL_PLATFORM_VERSION;
31+
}
32+
};

frontend/src/global.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export {};
2+
3+
declare global {
4+
interface Window {
5+
__INFISICAL_RUNTIME_ENV__?: {
6+
CAPTCHA_SITE_KEY?: string;
7+
POSTHOG_API_KEY?: string;
8+
INTERCOM_ID?: string;
9+
TELEMETRY_CAPTURING_ENABLED: string;
10+
};
11+
}
12+
}

0 commit comments

Comments
 (0)