abstracted config to service

This commit is contained in:
Wyatt Johnson
2018-01-31 11:25:43 -07:00
parent 062ce58cf8
commit c4cddefcff
4 changed files with 25 additions and 15 deletions
@@ -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;
+4 -1
View File
@@ -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
+3 -3
View File
@@ -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';
@@ -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;
};