Skip to content

Commit

Permalink
Merge branch 'main' into pdp-i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
herzog31 authored Jun 13, 2024
2 parents 5095c6d + b6b98b8 commit 38ee7b1
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions scripts/configs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const ALLOWED_CONFIGS = ['prod', 'stage', 'dev'];

/**
* This function calculates the environment in which the site is running based on the URL.
* It defaults to 'prod'. In non 'prod' environments, the value can be overwritten using
Expand All @@ -16,7 +18,7 @@ export const calcEnvironment = () => {
}

const environmentFromConfig = window.sessionStorage.getItem('environment');
if (environmentFromConfig && environment !== 'prod') {
if (environmentFromConfig && ALLOWED_CONFIGS.includes(environmentFromConfig) && environment !== 'prod') {
return environmentFromConfig;
}

Expand All @@ -25,16 +27,23 @@ export const calcEnvironment = () => {

function buildConfigURL(environment) {
const env = environment || calcEnvironment();
const configURL = new URL(`${window.location.origin}/configs.json`);
configURL.searchParams.set('sheet', env);
let fileName = 'configs.json?sheet=prod';
if (env !== 'prod') {
fileName = `configs-${env}.json`;
}
const configURL = new URL(`${window.location.origin}/${fileName}`);
return configURL;
}

const getConfigForEnvironment = async (environment) => {
const env = environment || calcEnvironment();
let configJSON = window.sessionStorage.getItem(`config:${env}`);
if (!configJSON) {
configJSON = await fetch(buildConfigURL(env)).then((res) => res.text());
configJSON = await fetch(buildConfigURL(env));
if (!configJSON.ok) {
throw new Error(`Failed to fetch config for ${env}`);
}
configJSON = await configJSON.text();
window.sessionStorage.setItem(`config:${env}`, configJSON);
}
return configJSON;
Expand Down

0 comments on commit 38ee7b1

Please sign in to comment.