Update how we rewrite the relative config.

Signed-off-by: Aramis Sennyey <sennyeya@amazon.com>
This commit is contained in:
Aramis Sennyey
2022-11-30 12:19:32 -05:00
parent 3ead02bc49
commit 120d60b39e
4 changed files with 44 additions and 29 deletions
+1 -1
View File
@@ -28,8 +28,8 @@ describe('App', () => {
app: {
title: 'Test',
support: { url: 'http://localhost:7007/support' },
baseUrl: 'http://localhost:3000',
},
backend: { baseUrl: 'http://localhost:7007' },
lighthouse: {
baseUrl: 'http://localhost:3003',
},
@@ -614,7 +614,7 @@ describe('Integration Test', () => {
],
[
[
`http://test-front.com/backstage/instance`,
`${document.location.origin}/backstage/instance`,
`http://test.com/backstage/instance`,
],
{
+40 -26
View File
@@ -82,6 +82,7 @@ import { ApiRegistry } from '../apis/system/ApiRegistry';
import { resolveRouteBindings } from './resolveRouteBindings';
import { BackstageRouteObject } from '../routing/types';
import { isReactRouterBeta } from './isReactRouterBeta';
import { JsonObject } from '@backstage/types';
type CompatiblePlugin =
| BackstagePlugin
@@ -155,12 +156,11 @@ function useConfigLoader(
}
let configReader;
/**
* config.value can be undefined or empty. If it's either, don't bother overriding anything.
*/
if (config.value?.length) {
configReader = ConfigReader.fromConfigs(config.value);
const urlConfigReader = ConfigReader.fromConfigs(config.value);
const resolveRelativeUrl = (relativeUrl: string) =>
new URL(relativeUrl, document.location.origin).href;
@@ -172,32 +172,46 @@ function useConfigLoader(
const getOrigin = (url: string) => new URL(url).origin;
const appBaseUrl = configReader.getString('app.baseUrl');
const backendBaseUrl = configReader.getString('backend.baseUrl');
const appOrigin = getOrigin(appBaseUrl);
const backendOrigin = getOrigin(backendBaseUrl);
const appBaseUrl = urlConfigReader.getOptionalString('app.baseUrl');
const backendBaseUrl = urlConfigReader.getOptionalString('backend.baseUrl');
let configs = config.value;
const relativeResolverConfig: { data: JsonObject; context: string } = {
data: {},
context: 'relative-resolver',
};
if (appBaseUrl && backendBaseUrl) {
const appOrigin = getOrigin(appBaseUrl);
const backendOrigin = getOrigin(backendBaseUrl);
/**
* We only want to override the URLs with the document origin when the URLs match
* and are defined. We use getOptionalString here to not throw when the app.baseUrl
* and backend.baseUrl are not defined. If they are defined but not well formatted URLs
* the above getRelativeUrl() method will throw.
*/
if (appOrigin === backendOrigin) {
config.value.push({
data: {
app: {
baseUrl: resolveRelativeUrl(getRelativeUrl(appBaseUrl)),
},
backend: {
baseUrl: resolveRelativeUrl(getRelativeUrl(backendBaseUrl)),
},
},
context: 'relative-resolver',
});
/**
* We only want to override the URLs with the document origin when the URLs match
* and are defined. We use getOptionalString here to not throw when the app.baseUrl
* and backend.baseUrl are not defined. If they are defined but not well formatted URLs
* the above getRelativeUrl() method will throw.
*/
if (appOrigin === backendOrigin) {
relativeResolverConfig.data.backend = {
baseUrl: resolveRelativeUrl(getRelativeUrl(backendBaseUrl)),
};
}
}
configReader = ConfigReader.fromConfigs(config.value);
if (appBaseUrl) {
/**
* Rewriting app.baseUrl to the current document should be a no-op. The
* document hosting the app should always be the same url as the app
* references.
*/
relativeResolverConfig.data.app = {
baseUrl: resolveRelativeUrl(getRelativeUrl(appBaseUrl)),
};
}
/**
* Only add the relative config if there is actually data to add.
*/
if (Object.keys(relativeResolverConfig.data).length) {
configs = configs.concat([relativeResolverConfig]);
}
configReader = ConfigReader.fromConfigs(configs);
} else {
configReader = ConfigReader.fromConfigs([]);
}
@@ -22,7 +22,8 @@ jest.mock('./config', () => ({
configLoader: async () => [
{
data: {
app: { title: 'Test', baseUrl: 'http://localhost:3000' },
app: { title: 'Test' },
backend: { baseUrl: 'http://localhost:7007' },
techdocs: {
storageUrl: 'http://localhost:7007/api/techdocs/static/docs',
},