-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose environment variables to client (#11375)
* Testing exposing clientside env vars * Test in Express app * import fix * Rename env variables object to be clearer and what it does * Remove `SIMORGH_APP_ENV` from `next.config.js` * Use initialiser method to allow `process.env` overrides - This is mainly to support tests that set the `process.env` values within the tests * Update index.ts * Test with `SIMORGH_ATI_BASE_URL` * Type check `SIMORGH_APP_ENV` * Update _document.page.tsx * Update index.ts * Update component.jsx * Update `process.env` references to use `getEnvConfig()` * Add missing cache config * Set ichef default back * Fix import * Update index.ts * Snapshot update * Test removing `getEnvConfig` from `getOriginContext` * Revert "Test removing `getEnvConfig` from `getOriginContext`" This reverts commit 81e26db. * Update index.ts * Test removing path aliasing * Add test * Improve tests * Update index.test.ts * Remove `process.env` from clientside test - These should be coming from the `window` object, so its more accurate to remove these
- Loading branch information
Showing
41 changed files
with
275 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
src/app/contexts/ToggleContext/utils/constructTogglesEndpoint/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { getEnvConfig } from '#app/lib/utilities/getEnvConfig'; | ||
import { Services } from '#app/models/types/global'; | ||
|
||
export default (service: Services, origin: string | null) => { | ||
const requestOrigin = origin || 'https://www.test.bbc.com'; | ||
const baseTogglesUrl = `${process.env.SIMORGH_CONFIG_URL}?application=simorgh&service=${service}&__amp_source_origin=${requestOrigin}`; // __amp_source_origin is relevant to both canonical and amp | ||
const baseTogglesUrl = `${ | ||
getEnvConfig().SIMORGH_CONFIG_URL | ||
}?application=simorgh&service=${service}&__amp_source_origin=${requestOrigin}`; // __amp_source_origin is relevant to both canonical and amp | ||
|
||
return baseTogglesUrl; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
import { getEnvConfig } from '../getEnvConfig'; | ||
|
||
const getBrandedImage = (locator, service) => | ||
`${process.env.SIMORGH_ICHEF_BASE_URL}/news/1024/branded_${service}/${locator}`; | ||
`${ | ||
getEnvConfig().SIMORGH_ICHEF_BASE_URL | ||
}/news/1024/branded_${service}/${locator}`; | ||
|
||
export default getBrandedImage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { getEnvConfig } from '.'; | ||
|
||
let windowSpy: jest.SpyInstance<Window | undefined, []>; | ||
|
||
describe('getEnvConfig', () => { | ||
const originalProcessEnv = process.env; | ||
|
||
beforeEach(() => { | ||
process.env = originalProcessEnv; | ||
windowSpy = jest.spyOn(window, 'window', 'get'); | ||
}); | ||
|
||
afterEach(() => { | ||
windowSpy.mockRestore(); | ||
}); | ||
|
||
it('server side - should return values from "getEnvConfig"', () => { | ||
process.env.SIMORGH_APP_ENV = 'test'; | ||
process.env.SIMORGH_BASE_URL = 'https://test.com'; | ||
|
||
// simulate server side by removing window object | ||
windowSpy.mockImplementation(() => undefined); | ||
|
||
const results = getEnvConfig(); | ||
|
||
expect(results.SIMORGH_APP_ENV).toEqual('test'); | ||
expect(results.SIMORGH_BASE_URL).toEqual('https://test.com'); | ||
}); | ||
|
||
it('client side - should return values from "getEnvConfig"', () => { | ||
// simulate client side by adding window object | ||
windowSpy.mockImplementation( | ||
() => | ||
({ | ||
location: { | ||
origin: 'https://test.com', | ||
}, | ||
SIMORGH_ENV_VARS: { | ||
SIMORGH_APP_ENV: 'test', | ||
SIMORGH_BASE_URL: 'https://test.com', | ||
}, | ||
} as Window), | ||
); | ||
|
||
const results = getEnvConfig(); | ||
|
||
expect(results.SIMORGH_APP_ENV).toEqual('test'); | ||
expect(results.SIMORGH_BASE_URL).toEqual('https://test.com'); | ||
expect(window.SIMORGH_ENV_VARS).toEqual({ | ||
SIMORGH_APP_ENV: 'test', | ||
SIMORGH_BASE_URL: 'https://test.com', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Environments } from '#app/models/types/global'; | ||
import onClient from '../onClient'; | ||
|
||
export type EnvConfig = ReturnType<typeof getProcessEnvAppVariables>; | ||
|
||
// Any environment variables added here will be available to the client and server | ||
export const getProcessEnvAppVariables = () => ({ | ||
SIMORGH_APP_ENV: process.env.SIMORGH_APP_ENV as Environments, | ||
SIMORGH_ATI_BASE_URL: process.env.SIMORGH_ATI_BASE_URL, | ||
SIMORGH_BASE_URL: process.env.SIMORGH_BASE_URL, | ||
SIMORGH_CONFIG_CACHE_ITEMS: process.env.SIMORGH_CONFIG_CACHE_ITEMS, | ||
SIMORGH_CONFIG_CACHE_MAX_AGE_SECONDS: | ||
process.env.SIMORGH_CONFIG_CACHE_MAX_AGE_SECONDS, | ||
SIMORGH_CONFIG_TIMEOUT_SECONDS: process.env.SIMORGH_CONFIG_TIMEOUT_SECONDS, | ||
SIMORGH_CONFIG_URL: process.env.SIMORGH_CONFIG_URL, | ||
SIMORGH_CSP_REPORTING_ENDPOINT: process.env.SIMORGH_CSP_REPORTING_ENDPOINT, | ||
SIMORGH_ICHEF_BASE_URL: process.env.SIMORGH_ICHEF_BASE_URL, | ||
SIMORGH_INCLUDES_BASE_URL: process.env.SIMORGH_INCLUDES_BASE_URL, | ||
SIMORGH_INCLUDES_BASE_AMP_URL: process.env.SIMORGH_INCLUDES_BASE_AMP_URL, | ||
SIMORGH_MOST_READ_CDN_URL: process.env.SIMORGH_MOST_READ_CDN_URL, | ||
SIMORGH_OPTIMIZELY_SDK_KEY: process.env.SIMORGH_OPTIMIZELY_SDK_KEY, | ||
SIMORGH_PUBLIC_STATIC_ASSETS_ORIGIN: | ||
process.env.SIMORGH_PUBLIC_STATIC_ASSETS_ORIGIN, | ||
SIMORGH_PUBLIC_STATIC_ASSETS_PATH: | ||
process.env.SIMORGH_PUBLIC_STATIC_ASSETS_PATH, | ||
SIMORGH_WEBVITALS_REPORTING_ENDPOINT: | ||
process.env.SIMORGH_WEBVITALS_REPORTING_ENDPOINT, | ||
SIMORGH_WEBVITALS_DEFAULT_SAMPLING_RATE: | ||
process.env.SIMORGH_WEBVITALS_DEFAULT_SAMPLING_RATE, | ||
}); | ||
|
||
export function getEnvConfig(): EnvConfig { | ||
// Return window object on client and when window.SIMORGH_ENV_VARS is set | ||
if (onClient() && window?.SIMORGH_ENV_VARS) { | ||
return window.SIMORGH_ENV_VARS; | ||
} | ||
|
||
// Return server side environment variables | ||
return getProcessEnvAppVariables(); | ||
} | ||
|
||
declare global { | ||
interface Window { | ||
SIMORGH_ENV_VARS: EnvConfig; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.