From ec513e93b2b49f232908a8e409f4087384e928f0 Mon Sep 17 00:00:00 2001 From: Dave Mulligan Date: Tue, 18 Jul 2023 11:36:06 +0100 Subject: [PATCH 1/2] fix: getContextPath did not work correctly when called in a browser --- libs/shared/src/config/contextPath.ts | 32 ++++++++++++++++++--------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/libs/shared/src/config/contextPath.ts b/libs/shared/src/config/contextPath.ts index e90baea01b3..6a0d4c8bc94 100644 --- a/libs/shared/src/config/contextPath.ts +++ b/libs/shared/src/config/contextPath.ts @@ -8,29 +8,41 @@ export enum NovuComponentEnum { export function getContextPath(component: NovuComponentEnum) { let contextPath = ''; - if (process.env.GLOBAL_CONTEXT_PATH) { - contextPath += process.env.GLOBAL_CONTEXT_PATH + '/'; + // Determine if we are running in the browser or in node.js. If we are + // running in node.js, we will have access to the process.env object, + // otherwise we will have access to the window._env_ object to get the + // environment variables. + + // eslint-disable-next-line no-undef + const env = typeof process !== 'undefined' && process?.env ? process?.env : (window as any)._env_; + + if (!env) { + return contextPath; + } + + if (env.GLOBAL_CONTEXT_PATH) { + contextPath += env.GLOBAL_CONTEXT_PATH + '/'; } switch (component) { case NovuComponentEnum.API: - if (process.env.API_CONTEXT_PATH) { - contextPath += process.env.API_CONTEXT_PATH + '/'; + if (env.API_CONTEXT_PATH) { + contextPath += env.API_CONTEXT_PATH + '/'; } break; case NovuComponentEnum.WEB: - if (process.env.FRONT_BASE_CONTEXT_PATH) { - contextPath += process.env.FRONT_BASE_CONTEXT_PATH + '/'; + if (env.FRONT_BASE_CONTEXT_PATH) { + contextPath += env.FRONT_BASE_CONTEXT_PATH + '/'; } break; case NovuComponentEnum.WIDGET: - if (process.env.WIDGET_CONTEXT_PATH) { - contextPath += process.env.WIDGET_CONTEXT_PATH + '/'; + if (env.WIDGET_CONTEXT_PATH) { + contextPath += env.WIDGET_CONTEXT_PATH + '/'; } break; case NovuComponentEnum.WS: - if (process.env.WS_CONTEXT_PATH) { - contextPath += process.env.WS_CONTEXT_PATH + '/'; + if (env.WS_CONTEXT_PATH) { + contextPath += env.WS_CONTEXT_PATH + '/'; } break; } From 645d53535d62383cabedcd7d2df6ca38717f5b05 Mon Sep 17 00:00:00 2001 From: Dave Mulligan Date: Thu, 20 Jul 2023 11:38:21 +0100 Subject: [PATCH 2/2] Updated comment --- libs/shared/src/config/contextPath.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libs/shared/src/config/contextPath.ts b/libs/shared/src/config/contextPath.ts index 6a0d4c8bc94..4940b77c18f 100644 --- a/libs/shared/src/config/contextPath.ts +++ b/libs/shared/src/config/contextPath.ts @@ -8,10 +8,12 @@ export enum NovuComponentEnum { export function getContextPath(component: NovuComponentEnum) { let contextPath = ''; - // Determine if we are running in the browser or in node.js. If we are - // running in node.js, we will have access to the process.env object, - // otherwise we will have access to the window._env_ object to get the - // environment variables. + /** + * Determine if we are running in the browser or in node.js. If we are + * running in node.js, we will have access to the process.env object, + * otherwise we will have access to the window._env_ object to get the + * environment variables. + */ // eslint-disable-next-line no-undef const env = typeof process !== 'undefined' && process?.env ? process?.env : (window as any)._env_;