diff --git a/config/environment.default.js b/config/environment.default.js index 47159c2797..5391908e52 100644 --- a/config/environment.default.js +++ b/config/environment.default.js @@ -4,12 +4,16 @@ module.exports = { "rest": { // NOTE: Space is capitalized because 'namespace' is a reserved string in TypeScript "nameSpace": "/api", - "baseURL": "http://localhost:3000" + "protocol": "http", + "address": "localhost", + "port": 3000 }, // Path and Port in use for this Angular2 UI "ui": { "nameSpace": "/", - "baseURL": "http://localhost:3000" + "protocol": "http", + "address": "localhost", + "port": 3000 }, "cache": { // how long should objects be cached for by default @@ -17,6 +21,8 @@ module.exports = { }, "universal": { //on the client: start with the state on the server - "shouldRehydrate": true + "shouldRehydrate": true, + "preboot": true, + "async": true } }; diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index 2664b5f27b..a9d1d6205e 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -21,6 +21,8 @@ import { HostWindowState } from "./shared/host-window.reducer"; import { HostWindowResizeAction } from "./shared/host-window.actions"; import { MockTranslateLoader } from "./shared/testing/mock-translate-loader"; +import { globalConfig } from '../config'; + let comp: AppComponent; let fixture: ComponentFixture; let de: DebugElement; @@ -37,7 +39,8 @@ describe('App component', () => { })], declarations: [AppComponent], // declare the test component providers: [ - AppComponent + AppComponent, + globalConfig ], schemas: [CUSTOM_ELEMENTS_SCHEMA] }) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 8586679931..eb5079a49d 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -45,6 +45,8 @@ export class AppComponent implements OnDestroy, OnInit { translate.setDefaultLang('en'); // the lang to use, if the lang isn't available, it will use the current loader to get them translate.use('en'); + + console.log(this.config); } ngOnInit() { diff --git a/src/config.ts b/src/config.ts index 7ae16bdf11..764db17fe3 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,7 +1,7 @@ // Look in ./config folder for config import { OpaqueToken } from '@angular/core'; -import path from 'path'; +import * as path from 'path'; let configContext = require.context("../config", false, /js$/); @@ -33,27 +33,43 @@ try { const GLOBAL_CONFIG = new OpaqueToken('config'); +interface ServerConfig { + "nameSpace": string, + "protocol": string, + "address": string, + "port": number, + "baseURL": string +} + interface GlobalConfig { "production": string, - "rest": { - "nameSpace": string, - "baseURL": string - }, - "ui": { - "nameSpace": string, - "baseURL": string - }, + "rest": ServerConfig, + "ui": ServerConfig, "cache": { "msToLive": number, }, "universal": { - "shouldRehydrate": boolean + "shouldRehydrate": boolean, + "preboot": boolean, + "async": boolean + } +} + +const config: GlobalConfig = Object.assign(DefaultConfig, EnvConfig); + +function buildURL(server: ServerConfig) { + return [server.protocol, '://', server.address, (server.port !== 80) ? ':' + server.port : ''].join(''); +} + +for (let key in config) { + if (config[key].protocol && config[key].address && config[key].port) { + config[key].baseURL = buildURL(config[key]); } } const globalConfig = { provide: GLOBAL_CONFIG, - useValue: Object.assign(DefaultConfig, EnvConfig) + useValue: config }; -export { GLOBAL_CONFIG, GlobalConfig, globalConfig} +export { GLOBAL_CONFIG, GlobalConfig, globalConfig, config } diff --git a/src/platform/modules/browser.module.ts b/src/platform/modules/browser.module.ts index 33b1df9410..d683a58626 100755 --- a/src/platform/modules/browser.module.ts +++ b/src/platform/modules/browser.module.ts @@ -45,7 +45,6 @@ export function getResponse() { return {}; } - export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE'; @NgModule({ @@ -89,17 +88,15 @@ export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE'; ] }) export class MainModule { - constructor(public store: Store, @Inject(GLOBAL_CONFIG) private config: GlobalConfig, ) { + constructor(public store: Store) { // TODO(gdi2290): refactor into a lifecycle hook this.doRehydrate(); } doRehydrate() { - if (this.config.universal.shouldRehydrate) { - let defaultValue = {}; - let serverCache = this._getCacheValue(NGRX_CACHE_KEY, defaultValue); - this.store.dispatch(new RehydrateStoreAction(serverCache)); - } + let defaultValue = {}; + let serverCache = this._getCacheValue(NGRX_CACHE_KEY, defaultValue); + this.store.dispatch(new RehydrateStoreAction(serverCache)); } _getCacheValue(key: string, defaultValue: any): any { diff --git a/src/server.aot.ts b/src/server.aot.ts index 9e02f38277..11c2143f52 100644 --- a/src/server.aot.ts +++ b/src/server.aot.ts @@ -25,6 +25,8 @@ import { MainModuleNgFactory } from './platform/modules/node.module.ngfactory'; // Routes import { routes } from './server.routes'; +import { config } from './config'; + // enable prod for faster renders enableProdMode(); diff --git a/src/server.ts b/src/server.ts index fdcf3c19d1..94f84259f3 100644 --- a/src/server.ts +++ b/src/server.ts @@ -24,6 +24,8 @@ import { MainModule } from './platform/modules/node.module'; // Routes import { routes } from './server.routes'; +import { config } from './config'; + // enable prod for faster renders enableProdMode();