Project

General

Profile

1
import { NgModule } from '@angular/core';
2
import { FormsModule } from '@angular/forms';
3
import { RouterModule } from '@angular/router';
4
import { UniversalModule, isBrowser, isNode, AUTO_PREBOOT } from 'angular2-universal/browser'; // for AoT we need to manually split universal packages
5

    
6
import { AppModule, AppComponent } from './app/app.module';
7
import { SharedModule } from './app/shared/shared.module';
8
import { CacheService } from './app/shared/cache.service';
9

    
10
// Will be merged into @angular/platform-browser in a later release
11
// see https://github.com/angular/angular/pull/12322
12
import { Meta } from './angular2-meta';
13

    
14
// import * as LRU from 'modern-lru';
15

    
16
export function getLRU(lru?: any) {
17
  // use LRU for node
18
  // return lru || new LRU(10);
19
  return lru || new Map();
20
}
21
export function getRequest() {
22
  // the request object only lives on the server
23
  return { cookie: document.cookie };
24
}
25
export function getResponse() {
26
  // the response object is sent as the index.html and lives on the server
27
  return {};
28
}
29

    
30

    
31
// TODO(gdi2290): refactor into Universal
32
export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
33

    
34
@NgModule({
35
  bootstrap: [ AppComponent ],
36
  imports: [
37
    // MaterialModule.forRoot() should be included first
38
    UniversalModule, // BrowserModule, HttpModule, and JsonpModule are included
39

    
40
    FormsModule,
41
    RouterModule.forRoot([], { useHash: false }),
42

    
43
    SharedModule.forRoot(),
44
    AppModule,
45
  ],
46
  providers: [
47
    { provide: 'isBrowser', useValue: isBrowser },
48
    { provide: 'isNode', useValue: isNode },
49

    
50
    { provide: 'req', useFactory: getRequest },
51
    { provide: 'res', useFactory: getResponse },
52

    
53
    { provide: 'LRU', useFactory: getLRU, deps: [] },
54

    
55
    CacheService,
56

    
57
    Meta,
58

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

    
68
  doRehydrate() {
69
    let defaultValue = {};
70
    let serverCache = this._getCacheValue(CacheService.KEY, defaultValue);
71
    this.cache.rehydrate(serverCache);
72
  }
73

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