diff --git a/src/PUBLIC_URL.ts b/src/PUBLIC_URL.ts new file mode 100644 index 00000000..cfd6d701 --- /dev/null +++ b/src/PUBLIC_URL.ts @@ -0,0 +1,13 @@ +import { nameOfTheGlobal, basenameOfTheKeycloakifyResourcesDir } from "keycloakify/bin/constants"; + +/** + * This is an equivalent of process.env.PUBLIC_URL thay you can use in Webpack projects. + * This works both in your main app and in your Keycloak theme. + */ +export const PUBLIC_URL = (() => { + const kcContext = (window as any)[nameOfTheGlobal]; + + return kcContext === undefined || process.env.NODE_ENV === "development" + ? process.env.PUBLIC_URL + : `${kcContext.url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}`; +})();