1
|
//import {MaterialModule} from '@angular/material';
|
2
|
import { NgModule } from '@angular/core';
|
3
|
import { FormsModule } from '@angular/forms';
|
4
|
import { RouterModule } from '@angular/router';
|
5
|
import { UniversalModule, isBrowser, isNode, AUTO_PREBOOT } from 'angular2-universal/browser'; // for AoT we need to manually split universal packages
|
6
|
import { IdlePreload, IdlePreloadModule } from '@angularclass/idle-preload';
|
7
|
|
8
|
import { AppModule, AppComponent } from './app/app.module';
|
9
|
import { SharedModule } from './app/shared/shared.module';
|
10
|
import { CacheService } from './app/shared/cache.service';
|
11
|
|
12
|
// Will be merged into @angular/platform-browser in a later release
|
13
|
// see https://github.com/angular/angular/pull/12322
|
14
|
import { Meta } from './angular2-meta';
|
15
|
|
16
|
// import * as LRU from 'modern-lru';
|
17
|
|
18
|
export function getLRU(lru?: any) {
|
19
|
// use LRU for node
|
20
|
// return lru || new LRU(10);
|
21
|
return lru || new Map();
|
22
|
}
|
23
|
export function getRequest() {
|
24
|
// the request object only lives on the server
|
25
|
return { cookie: document.cookie };
|
26
|
}
|
27
|
export function getResponse() {
|
28
|
// the response object is sent as the index.html and lives on the server
|
29
|
return {};
|
30
|
}
|
31
|
|
32
|
|
33
|
// TODO(gdi2290): refactor into Universal
|
34
|
export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
|
35
|
|
36
|
@NgModule({
|
37
|
bootstrap: [ AppComponent ],
|
38
|
imports: [
|
39
|
//MaterialModule.forRoot(),// should be included first
|
40
|
UniversalModule, // BrowserModule, HttpModule, and JsonpModule are included
|
41
|
|
42
|
FormsModule,
|
43
|
RouterModule.forRoot([], { useHash: false, preloadingStrategy: IdlePreload }),
|
44
|
|
45
|
IdlePreloadModule.forRoot(),
|
46
|
SharedModule.forRoot(),
|
47
|
AppModule,
|
48
|
],
|
49
|
providers: [
|
50
|
{ provide: 'isBrowser', useValue: isBrowser },
|
51
|
{ provide: 'isNode', useValue: isNode },
|
52
|
{ provide: 'req', useFactory: getRequest },
|
53
|
{ provide: 'res', useFactory: getResponse },
|
54
|
{ provide: 'LRU', useFactory: getLRU, deps: [] },
|
55
|
CacheService,
|
56
|
Meta
|
57
|
|
58
|
// { provide: AUTO_PREBOOT, useValue: false } // turn off auto preboot complete
|
59
|
]
|
60
|
})
|
61
|
export class MainModule {
|
62
|
constructor(public cache: CacheService) {
|
63
|
// TODO(gdi2290): refactor into a lifecycle hook
|
64
|
this.doRehydrate();
|
65
|
}
|
66
|
|
67
|
doRehydrate() {
|
68
|
let defaultValue = {};
|
69
|
let serverCache = this._getCacheValue(CacheService.KEY, defaultValue);
|
70
|
this.cache.rehydrate(serverCache);
|
71
|
}
|
72
|
|
73
|
_getCacheValue(key: string, defaultValue: any): any {
|
74
|
// browser
|
75
|
const win: any = window;
|
76
|
if (win[UNIVERSAL_KEY] && win[UNIVERSAL_KEY][key]) {
|
77
|
let serverCache = defaultValue;
|
78
|
try {
|
79
|
serverCache = JSON.parse(win[UNIVERSAL_KEY][key]);
|
80
|
if (typeof serverCache !== typeof defaultValue) {
|
81
|
console.log('Angular Universal: The type of data from the server is different from the default value type');
|
82
|
serverCache = defaultValue;
|
83
|
}
|
84
|
} catch (e) {
|
85
|
console.log('Angular Universal: There was a problem parsing the server data during rehydrate');
|
86
|
serverCache = defaultValue;
|
87
|
}
|
88
|
return serverCache;
|
89
|
} else {
|
90
|
console.log('Angular Universal: UNIVERSAL_CACHE is missing');
|
91
|
}
|
92
|
return defaultValue;
|
93
|
}
|
94
|
}
|