|
1 |
| -import 'angular2-universal/polyfills'; |
2 |
| -import * as ngCore from '@angular/core'; |
3 |
| -import { APP_BASE_HREF } from '@angular/common'; |
4 |
| -import { provideRouter } from '@angular/router'; |
5 |
| -import * as ngUniversal from 'angular2-universal'; |
6 |
| -import { BASE_URL, ORIGIN_URL, REQUEST_URL } from 'angular2-universal/common'; |
7 |
| -import { App } from './components/app/app'; |
| 1 | +// the polyfills must be the first thing imported in node.js |
| 2 | +import 'angular2-universal-polyfills'; |
| 3 | + |
| 4 | +// Angular 2 |
| 5 | +import { enableProdMode } from '@angular/core'; |
| 6 | +// Angular2 Universal |
| 7 | +import { platformNodeDynamic } from 'angular2-universal'; |
| 8 | + |
| 9 | +// Application imports |
| 10 | +import { MainModule } from './main.node'; |
| 11 | +import { App } from './components'; |
8 | 12 | import { routes } from './routes';
|
9 | 13 |
|
10 |
| -const bootloader = ngUniversal.bootloader({ |
11 |
| - async: true, |
12 |
| - preboot: false, |
13 |
| - platformProviders: [ |
14 |
| - ngCore.provide(APP_BASE_HREF, { useValue: '/' }), |
15 |
| - ] |
16 |
| -}); |
17 |
| - |
18 |
| -export default function (params: any): Promise<{ html: string, globals?: any }> { |
19 |
| - const config: ngUniversal.AppConfig = { |
20 |
| - directives: [App], |
21 |
| - providers: [ |
22 |
| - ngCore.provide(ORIGIN_URL, { useValue: params.origin }), |
23 |
| - ngCore.provide(REQUEST_URL, { useValue: params.url }), |
24 |
| - ...ngUniversal.NODE_HTTP_PROVIDERS, |
25 |
| - provideRouter(routes), |
26 |
| - ...ngUniversal.NODE_LOCATION_PROVIDERS, |
27 |
| - ], |
28 |
| - // TODO: Render just the <app> component instead of wrapping it inside an extra HTML document |
29 |
| - // Waiting on https://github.com/angular/universal/issues/347 |
30 |
| - template: '<!DOCTYPE html>\n<html><head></head><body><app></app></body></html>' |
| 14 | +// enable prod for faster renders |
| 15 | +enableProdMode(); |
| 16 | + |
| 17 | +declare var Zone: any; |
| 18 | + |
| 19 | +export default function (params: any) : Promise<{ html: string, globals?: any }> { |
| 20 | + |
| 21 | + const doc = ` |
| 22 | + <!DOCTYPE html>\n |
| 23 | + <html> |
| 24 | + <head></head> |
| 25 | + <body> |
| 26 | + <app></app> |
| 27 | + </body> |
| 28 | + </html> |
| 29 | + `; |
| 30 | + |
| 31 | + // hold platform reference |
| 32 | + var platformRef = platformNodeDynamic(); |
| 33 | + |
| 34 | + var platformConfig = { |
| 35 | + ngModule: MainModule, |
| 36 | + document: doc, |
| 37 | + preboot: false, |
| 38 | + baseUrl: '/', |
| 39 | + requestUrl: params.url, |
| 40 | + originUrl: params.origin |
31 | 41 | };
|
32 | 42 |
|
33 |
| - return bootloader.serializeApplication(config).then(html => { |
| 43 | + // defaults |
| 44 | + var cancel = false; |
| 45 | + |
| 46 | + const _config = Object.assign({ |
| 47 | + get cancel() { return cancel; }, |
| 48 | + cancelHandler() { return Zone.current.get('cancel') } |
| 49 | + }, platformConfig); |
| 50 | + |
| 51 | + // for each user |
| 52 | + const zone = Zone.current.fork({ |
| 53 | + name: 'UNIVERSAL request', |
| 54 | + properties: _config |
| 55 | + }); |
| 56 | + |
| 57 | + |
| 58 | + return Promise.resolve( |
| 59 | + zone.run(() => { |
| 60 | + return platformRef.serializeModule(Zone.current.get('ngModule')) |
| 61 | + }) |
| 62 | + ).then(html => { |
| 63 | + |
| 64 | + if (typeof html !== 'string' ) { |
| 65 | + return { html : doc }; |
| 66 | + } |
34 | 67 | return { html };
|
| 68 | + |
| 69 | + }).catch(err => { |
| 70 | + |
| 71 | + console.log(err); |
| 72 | + return { html : doc }; |
| 73 | + |
35 | 74 | });
|
| 75 | + |
36 | 76 | }
|
0 commit comments