From d69ca0a5086a932d671b1d292a1baea4a57c8e54 Mon Sep 17 00:00:00 2001 From: andreas-unleash Date: Wed, 7 Dec 2022 14:31:27 +0200 Subject: [PATCH] ui flags replaced in index.html (#2617) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: andreas-unleash This PR puts the ui flags in the index.html in a meta tag. This makes them accessible without logging in and allows us to track user signups with invite links ## About the changes Closes # ### Important files ## Discussion points Signed-off-by: andreas-unleash --- frontend/index.html | 1 + .../PlausibleProvider/PlausibleProvider.tsx | 19 ++++++++++++++++++- src/lib/util/load-index-html.ts | 3 ++- src/lib/util/rewriteHTML.ts | 3 +++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/frontend/index.html b/frontend/index.html index a7fa6b0b73..a74fbe07c1 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -6,6 +6,7 @@ + Unleash diff --git a/frontend/src/component/providers/PlausibleProvider/PlausibleProvider.tsx b/frontend/src/component/providers/PlausibleProvider/PlausibleProvider.tsx index 3022827eb8..08420407df 100644 --- a/frontend/src/component/providers/PlausibleProvider/PlausibleProvider.tsx +++ b/frontend/src/component/providers/PlausibleProvider/PlausibleProvider.tsx @@ -11,8 +11,25 @@ export const PlausibleProvider: FC = ({ children }) => { const [context, setContext] = useState | null>( null ); + + const getUIFlags = () => { + try { + const uiFlagsStr = + ( + document.querySelector( + 'meta[name="uiFlags"]' + ) as HTMLMetaElement + )?.content || '{}'; + return JSON.parse(uiFlagsStr); + } catch (e) { + return {}; + } + }; + + const uiFlags = getUIFlags(); + const { uiConfig } = useUiConfig(); - const isEnabled = Boolean(uiConfig?.flags?.T || LOCAL_TESTING); + const isEnabled = Boolean(uiConfig?.flags?.T || uiFlags.T || LOCAL_TESTING); useEffect(() => { if (isEnabled) { diff --git a/src/lib/util/load-index-html.ts b/src/lib/util/load-index-html.ts index 8ba5c5733f..9f732c379a 100644 --- a/src/lib/util/load-index-html.ts +++ b/src/lib/util/load-index-html.ts @@ -9,6 +9,7 @@ export async function loadIndexHTML( publicFolder: string, ): Promise { const { cdnPrefix, baseUriPath = '' } = config.server; + const uiFlags = JSON.stringify(config.ui.flags); let indexHTML: string; if (cdnPrefix) { @@ -20,5 +21,5 @@ export async function loadIndexHTML( .toString(); } - return rewriteHTML(indexHTML, baseUriPath, cdnPrefix); + return rewriteHTML(indexHTML, baseUriPath, cdnPrefix, uiFlags); } diff --git a/src/lib/util/rewriteHTML.ts b/src/lib/util/rewriteHTML.ts index 55eaff0dd4..6f05dd1e14 100644 --- a/src/lib/util/rewriteHTML.ts +++ b/src/lib/util/rewriteHTML.ts @@ -2,6 +2,7 @@ export const rewriteHTML = ( input: string, rewriteValue: string, cdnPrefix?: string, + uiFlags?: string, ): string => { let result = input; result = result.replace(/::baseUriPath::/gi, rewriteValue); @@ -10,6 +11,8 @@ export const rewriteHTML = ( const faviconPrefix = cdnPrefix ? 'https://cdn.getunleash.io' : ''; result = result.replace(/::faviconPrefix::/gi, faviconPrefix); + result = result.replace(/::uiFlags::/gi, uiFlags); + result = result.replace( /\/static/gi, `${cdnPrefix || rewriteValue}/static`,