Project

General

Profile

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

    
53
    { provide: 'req', useFactory: getRequest },
54
    { provide: 'res', useFactory: getResponse },
55

    
56
    { provide: 'LRU', useFactory: getLRU, deps: [] },
57

    
58
    CacheService,
59

    
60
    //Meta,
61

    
62
    // { provide: AUTO_PREBOOT, useValue: false } // turn off auto preboot complete
63
  ]
64
})
65
export class MainModule {
66
  constructor(public cache: CacheService) {
67
    // TODO(gdi2290): refactor into a lifecycle hook
68
    this.doRehydrate();
69
  }
70

    
71
  doRehydrate() {
72
    let defaultValue = {};
73
    let serverCache = this._getCacheValue(CacheService.KEY, defaultValue);
74
    this.cache.rehydrate(serverCache);
75
  }
76

    
77
  _getCacheValue(key: string, defaultValue: any): any {
78
    // browser
79
    const win: any = window;
80
    if (win[UNIVERSAL_KEY] && win[UNIVERSAL_KEY][key]) {
81
      let serverCache = defaultValue;
82
      try {
83
        serverCache = JSON.parse(win[UNIVERSAL_KEY][key]);
84
        if (typeof serverCache !== typeof defaultValue) {
85
          console.log('Angular Universal: The type of data from the server is different from the default value type');
86
          serverCache = defaultValue;
87
        }
88
      } catch (e) {
89
        console.log('Angular Universal: There was a problem parsing the server data during rehydrate');
90
        serverCache = defaultValue;
91
      }
92
      return serverCache;
93
    } else {
94
      console.log('Angular Universal: UNIVERSAL_CACHE is missing');
95
    }
96
    return defaultValue;
97
  }
98
}
(4-4/12)