keycloak_theme/src/PUBLIC_URL.ts

24 lines
870 B
TypeScript
Raw Normal View History

2024-09-08 12:00:07 +02:00
import { WELL_KNOWN_DIRECTORY_BASE_NAME } from "keycloakify/bin/shared/constants";
2024-02-11 10:36:50 +01:00
import { assert } from "tsafe/assert";
/**
* This is an equivalent of process.env.PUBLIC_URL that you can use in Webpack projects.
* This works both in your main app and in your Keycloak theme.
*/
export const PUBLIC_URL = (() => {
2024-09-08 12:00:07 +02:00
const kcContext: { "x-keycloakify": { resourcesPath: string } } | undefined = (
window as any
).kcContext;
2024-02-11 10:36:50 +01:00
if (kcContext === undefined || process.env.NODE_ENV === "development") {
assert(
process.env.PUBLIC_URL !== undefined,
`If you use keycloakify/PUBLIC_URL you should be in Webpack and thus process.env.PUBLIC_URL should be defined`
);
return process.env.PUBLIC_URL;
}
2024-09-08 12:00:07 +02:00
return `${kcContext["x-keycloakify"].resourcesPath}/${WELL_KNOWN_DIRECTORY_BASE_NAME.DIST}`;
})();