Skip to content

Commit

Permalink
ui flags replaced in index.html (#2617)
Browse files Browse the repository at this point in the history
Signed-off-by: andreas-unleash <andreas@getunleash.ai>

<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->
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
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Closes #

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
  • Loading branch information
andreas-unleash committed Dec 7, 2022
1 parent 8fed007 commit d69ca0a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions frontend/index.html
Expand Up @@ -6,6 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="baseUriPath" content="::baseUriPath::" />
<meta name="cdnPrefix" content="::cdnPrefix::" />
<meta name="uiFlags" content="::uiFlags::" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="unleash" />
<title>Unleash</title>
Expand Down
Expand Up @@ -11,8 +11,25 @@ export const PlausibleProvider: FC = ({ children }) => {
const [context, setContext] = useState<ReturnType<typeof Plausible> | 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) {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/util/load-index-html.ts
Expand Up @@ -9,6 +9,7 @@ export async function loadIndexHTML(
publicFolder: string,
): Promise<string> {
const { cdnPrefix, baseUriPath = '' } = config.server;
const uiFlags = JSON.stringify(config.ui.flags);

let indexHTML: string;
if (cdnPrefix) {
Expand All @@ -20,5 +21,5 @@ export async function loadIndexHTML(
.toString();
}

return rewriteHTML(indexHTML, baseUriPath, cdnPrefix);
return rewriteHTML(indexHTML, baseUriPath, cdnPrefix, uiFlags);
}
3 changes: 3 additions & 0 deletions src/lib/util/rewriteHTML.ts
Expand Up @@ -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);
Expand All @@ -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`,
Expand Down

0 comments on commit d69ca0a

Please sign in to comment.