From bf32f45298980215ec6c0cff6711df09d5cb4c90 Mon Sep 17 00:00:00 2001 From: William Welling Date: Fri, 24 Mar 2017 22:36:15 -0500 Subject: [PATCH] Cleanup and minore refactor. --- config/environment.default.js | 9 +++++---- src/app/app.component.scss | 3 +-- src/app/core/data-services/data.effects.ts | 10 +++++----- src/config.ts | 8 ++++---- src/platform/modules/browser.module.ts | 10 ++++++---- src/server.aot.ts | 4 ++-- src/server.ts | 4 ++-- 7 files changed, 25 insertions(+), 23 deletions(-) diff --git a/config/environment.default.js b/config/environment.default.js index 53ad5e9c0f..2cfae424d7 100644 --- a/config/environment.default.js +++ b/config/environment.default.js @@ -1,5 +1,5 @@ module.exports = { - // The REST API Location. + // The REST API server settings. "rest": { // NOTE: Space is capitalized because 'namespace' is a reserved string in TypeScript "nameSpace": "/api", @@ -7,7 +7,7 @@ module.exports = { "address": "localhost", "port": 3000 }, - // Protocol, path and port in use for this Angular2 UI + // Angular2 UI server settings. "ui": { "nameSpace": "/", "protocol": "http", @@ -16,10 +16,11 @@ module.exports = { }, "cache": { // how long should objects be cached for by default - "msToLive": 1 * 60 * 1000, // 1 minute + "msToLive": 15 * 60 * 1000, // 15 minute + "control": "max-age=60" // revalidate browser }, "universal": { - // universal settings + // Angular Universal settings "preboot": true, "async": true } diff --git a/src/app/app.component.scss b/src/app/app.component.scss index 4222d4b9e6..2a58ae0aa2 100644 --- a/src/app/app.component.scss +++ b/src/app/app.component.scss @@ -1,5 +1,4 @@ // Sticky Footer - .outer-wrapper { display: flex; margin: 0; @@ -23,4 +22,4 @@ h2.red { h2.green { color: green; -} \ No newline at end of file +} diff --git a/src/app/core/data-services/data.effects.ts b/src/app/core/data-services/data.effects.ts index 162d588981..107ad7eca3 100644 --- a/src/app/core/data-services/data.effects.ts +++ b/src/app/core/data-services/data.effects.ts @@ -21,7 +21,7 @@ export abstract class DataEffects { protected abstract getSerializer(): Serializer; constructor( - private config: GlobalConfig, + private EnvConfig: GlobalConfig, private actions$: Actions, private restApi: DSpaceRESTv2Service, private objectCache: ObjectCacheService, @@ -41,11 +41,11 @@ export abstract class DataEffects { if (hasNoValue(t) || hasNoValue(t.uuid)) { throw new Error('The server returned an invalid object'); } - this.objectCache.add(t, this.config.cache.msToLive); + this.objectCache.add(t, this.EnvConfig.cache.msToLive); }); }) .map((ts: Array) => ts.map(t => t.uuid)) - .map((ids: Array) => new RequestCacheSuccessAction(action.payload.key, ids, new Date().getTime(), this.config.cache.msToLive)) + .map((ids: Array) => new RequestCacheSuccessAction(action.payload.key, ids, new Date().getTime(), this.EnvConfig.cache.msToLive)) .catch((error: Error) => Observable.of(new RequestCacheErrorAction(action.payload.key, error.message))); }); @@ -59,9 +59,9 @@ export abstract class DataEffects { if (hasNoValue(t) || hasNoValue(t.uuid)) { throw new Error('The server returned an invalid object'); } - this.objectCache.add(t, this.config.cache.msToLive); + this.objectCache.add(t, this.EnvConfig.cache.msToLive); }) - .map((t: T) => new RequestCacheSuccessAction(action.payload.key, [t.uuid], new Date().getTime(), this.config.cache.msToLive)) + .map((t: T) => new RequestCacheSuccessAction(action.payload.key, [t.uuid], new Date().getTime(), this.EnvConfig.cache.msToLive)) .catch((error: Error) => Observable.of(new RequestCacheErrorAction(action.payload.key, error.message))); }); diff --git a/src/config.ts b/src/config.ts index 78f6d11446..1da3092069 100644 --- a/src/config.ts +++ b/src/config.ts @@ -15,9 +15,9 @@ interface GlobalConfig { "ui": ServerConfig, "cache": { "msToLive": number, + "control": string }, "universal": { - "shouldRehydrate": boolean, "preboot": boolean, "async": boolean } @@ -36,7 +36,7 @@ switch (process.env.NODE_ENV) { case 'prod': case 'production': production = true; - EnvConfigFile = './environment.prod.js'; + EnvConfigFile = './environment.prod.js'; break; case 'dev': case 'development': @@ -56,9 +56,9 @@ try { } // if EnvConfigFile set try to get configs -if(EnvConfigFile) { +if (EnvConfigFile) { try { - EnvConfig = Object.assign(EnvConfig, configContext(EnvConfigFile)); + EnvConfig = Object.assign(EnvConfig, configContext(EnvConfigFile)); } catch (e) { console.warn("Cannot find file " + EnvConfigFile); } diff --git a/src/platform/modules/browser.module.ts b/src/platform/modules/browser.module.ts index 9cb2178fd9..b3d809e852 100755 --- a/src/platform/modules/browser.module.ts +++ b/src/platform/modules/browser.module.ts @@ -23,7 +23,7 @@ import { effects } from '../../app/app.effects'; import { Meta } from '../angular2-meta'; import { RehydrateStoreAction } from "../../app/store.actions"; -import { GLOBAL_CONFIG, EnvConfig } from '../../config'; +import { GLOBAL_CONFIG, GlobalConfig, EnvConfig } from '../../config'; // import * as LRU from 'modern-lru'; @@ -73,7 +73,7 @@ export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE'; ], providers: [ { provide: GLOBAL_CONFIG, useValue: EnvConfig }, - + { provide: 'isBrowser', useValue: isBrowser }, { provide: 'isNode', useValue: isNode }, @@ -88,7 +88,7 @@ export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE'; ] }) export class MainModule { - constructor(public store: Store) { + constructor( @Inject(GLOBAL_CONFIG) private EnvConfig: GlobalConfig, public store: Store) { // TODO(gdi2290): refactor into a lifecycle hook this.doRehydrate(); } @@ -96,7 +96,9 @@ export class MainModule { doRehydrate() { let defaultValue = {}; let serverCache = this._getCacheValue(NGRX_CACHE_KEY, defaultValue); - this.store.dispatch(new RehydrateStoreAction(serverCache)); + if (this.EnvConfig.universal.preboot) { + 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 c8aa6a4c9a..ce450ec8e8 100644 --- a/src/server.aot.ts +++ b/src/server.aot.ts @@ -64,7 +64,7 @@ app.use(morgan('common', { function cacheControl(req, res, next) { // instruct browser to revalidate in 60 seconds - res.header('Cache-Control', 'max-age=60'); + res.header('Cache-Control', EnvConfig.cache.control || 'max-age=60'); next(); } @@ -115,7 +115,7 @@ routes.forEach(route => { app.get(`/${route}/*`, ngApp); }); -app.get('*', function(req, res) { +app.get('*', function (req, res) { res.setHeader('Content-Type', 'application/json'); var pojo = { status: 404, message: 'No Content' }; var json = JSON.stringify(pojo, null, 2); diff --git a/src/server.ts b/src/server.ts index 302462418e..6501cc390c 100644 --- a/src/server.ts +++ b/src/server.ts @@ -57,7 +57,7 @@ app.use(morgan('dev')); function cacheControl(req, res, next) { // instruct browser to revalidate in 60 seconds - res.header('Cache-Control', 'max-age=60'); + res.header('Cache-Control', EnvConfig.cache.control || 'max-age=60'); next(); } @@ -107,7 +107,7 @@ routes.forEach(route => { app.get(`/${route}/*`, ngApp); }); -app.get('*', function(req, res) { +app.get('*', function (req, res) { res.setHeader('Content-Type', 'application/json'); var pojo = { status: 404, message: 'No Content' }; var json = JSON.stringify(pojo, null, 2);