diff --git a/client/coral-framework/constants/config.js b/client/coral-framework/constants/config.js deleted file mode 100644 index 5ffc59ee2..000000000 --- a/client/coral-framework/constants/config.js +++ /dev/null @@ -1,11 +0,0 @@ -// This is always loaded in the body of the page, meaning that the dom element -// for this data blob is always loaded when we render this piece. -const CONFIG_ELEMENT = document.querySelector('#data'); - -// Parse the configuration from that element if it exists, otherwise, an empty -// object. -const CONFIG = CONFIG_ELEMENT ? JSON.parse(CONFIG_ELEMENT.textContent) : {}; - -// Export the expected fields. -export const LIVE_URI = CONFIG.LIVE_URI; -export const STATIC_URL = CONFIG.STATIC_URL; diff --git a/client/coral-framework/helpers/publicPath.js b/client/coral-framework/helpers/publicPath.js index d67166de9..57b6b7d15 100644 --- a/client/coral-framework/helpers/publicPath.js +++ b/client/coral-framework/helpers/publicPath.js @@ -1,6 +1,9 @@ /* global __webpack_public_path__ */ // eslint-disable-line no-unused-vars -import { STATIC_URL } from 'coral-framework/constants/config'; +import { getStaticConfiguration } from 'coral-framework/services/staticConfiguration'; + +// Load the static url from the static configuration. +const { STATIC_URL } = getStaticConfiguration(); // Update the static url for the imported public path so dynamically imported // chunks will use the correct path as defined by the process.env.STATIC_URL diff --git a/client/coral-framework/services/bootstrap.js b/client/coral-framework/services/bootstrap.js index b56872985..66da3ff29 100644 --- a/client/coral-framework/services/bootstrap.js +++ b/client/coral-framework/services/bootstrap.js @@ -1,4 +1,4 @@ -import { LIVE_URI } from 'coral-framework/constants/config'; +import { getStaticConfiguration } from 'coral-framework/services/staticConfiguration'; import { createStore } from './store'; import { createClient, apolloErrorReporter } from './client'; import pym from './pym'; @@ -83,8 +83,8 @@ export async function createContext({ token, }); - let liveUri = null; - if (LIVE_URI == null) { + let { LIVE_URI: liveUri } = getStaticConfiguration(); + if (liveUri == null) { // The protocol must match the origin protocol, secure/insecure. const protocol = location.protocol === 'https:' ? 'wss' : 'ws'; diff --git a/client/coral-framework/services/staticConfiguration.js b/client/coral-framework/services/staticConfiguration.js new file mode 100644 index 000000000..25eb606c7 --- /dev/null +++ b/client/coral-framework/services/staticConfiguration.js @@ -0,0 +1,18 @@ +/** + * getStaticConfiguration will return a singleton of the static configuration + * object provided via a JSON DOM element. + */ +let staticConfiguration = null; +export const getStaticConfiguration = () => { + if (staticConfiguration != null) { + return staticConfiguration; + } + + const configElement = document.querySelector('#data'); + + staticConfiguration = JSON.parse( + configElement ? configElement.textContent : '{}' + ); + + return staticConfiguration; +};