Skip to content

Commit

Permalink
fix(ui): update config at runtime to apply branding
Browse files Browse the repository at this point in the history
Signed-off-by: Yi Cai <yicai@redhat.com>
  • Loading branch information
ciiay committed Jul 11, 2024
1 parent 1d2edb1 commit fd34a50
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
5 changes: 1 addition & 4 deletions packages/app/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@
href="<%= publicPath %>/safari-pinned-tab.svg"
color="#5bbad5"
/>
<link
rel="icon"
href="<%= config.getOptionalString('app.branding.iconLogo') %>"
/>
<link rel="icon" id="dynamic-favicon" href="/favicon.ico" />
<title><%= config.getOptionalString('app.title') ?? 'Backstage' %></title>
</head>
<body>
Expand Down
2 changes: 2 additions & 0 deletions packages/app/src/components/AppBase/AppBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { entityPage } from '../catalog/EntityPage';
import { HomePage } from '../home/HomePage';
import { LearningPaths } from '../learningPaths/LearningPathsPage';
import { SearchPage } from '../search/SearchPage';
import ConfigUpdater from '../Root/ConfigUpdater';

const AppBase = () => {
const {
Expand Down Expand Up @@ -73,6 +74,7 @@ const AppBase = () => {
<AlertDisplay />
<OAuthRequestDialog />
<AppRouter>
<ConfigUpdater />
<Root>
<FlatRoutes>
{dynamicRoutes.filter(({ path }) => path === '/').length === 0 && (
Expand Down
35 changes: 35 additions & 0 deletions packages/app/src/components/Root/ConfigUpdater.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { useEffect } from 'react';
import { configApiRef, useApi } from '@backstage/core-plugin-api';

const ConfigUpdater: React.FC = () => {
const configApi = useApi(configApiRef);

useEffect(() => {
const updateConfig = () => {
const logoIconBase64URI = configApi.getOptionalString(
'app.branding.iconLogo',
);

if (logoIconBase64URI) {
const favicon = document.getElementById(
'dynamic-favicon',
) as HTMLLinkElement;
if (favicon) {
favicon.href = logoIconBase64URI;
} else {
const newFavicon = document.createElement('link');
newFavicon.id = 'dynamic-favicon';
newFavicon.rel = 'icon';
newFavicon.href = logoIconBase64URI;
document.head.appendChild(newFavicon);
}
}
};

updateConfig();
}, [configApi]);

return null;
};

export default ConfigUpdater;

0 comments on commit fd34a50

Please sign in to comment.