feat(platform-server): add API to render Module and ModuleFactory to string (#14381)
- PlatformState provides an interface to serialize the current Platform State as a string or Document. - renderModule and renderModuleFactory are convenience methods to wait for Angular Application to stabilize and then render the state to a string. - refactor code to remove defaultDoc from DomAdapter and inject DOCUMENT where it's needed.
This commit is contained in:
@ -7,19 +7,15 @@
|
||||
*/
|
||||
|
||||
import {PlatformLocation} from '@angular/common';
|
||||
import {Component, NgModule, destroyPlatform} from '@angular/core';
|
||||
import {async} from '@angular/core/testing';
|
||||
import {ApplicationRef, CompilerFactory, Component, NgModule, NgModuleRef, PlatformRef, destroyPlatform, getPlatform} from '@angular/core';
|
||||
import {async, inject} from '@angular/core/testing';
|
||||
import {DOCUMENT} from '@angular/platform-browser';
|
||||
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
||||
import {ServerModule, platformDynamicServer} from '@angular/platform-server';
|
||||
|
||||
function writeBody(html: string): any {
|
||||
const dom = getDOM();
|
||||
const doc = dom.defaultDoc();
|
||||
const body = dom.querySelector(doc, 'body');
|
||||
dom.setInnerHTML(body, html);
|
||||
return body;
|
||||
}
|
||||
|
||||
import {INITIAL_CONFIG, PlatformState, ServerModule, platformDynamicServer, renderModule, renderModuleFactory} from '@angular/platform-server';
|
||||
import {Subscription} from 'rxjs/Subscription';
|
||||
import {filter} from 'rxjs/operator/filter';
|
||||
import {first} from 'rxjs/operator/first';
|
||||
import {toPromise} from 'rxjs/operator/toPromise';
|
||||
|
||||
@Component({selector: 'app', template: `Works!`})
|
||||
class MyServerApp {
|
||||
@ -38,43 +34,117 @@ export function main() {
|
||||
afterEach(() => destroyPlatform());
|
||||
|
||||
it('should bootstrap', async(() => {
|
||||
const body = writeBody('<app></app>');
|
||||
platformDynamicServer().bootstrapModule(ExampleModule).then(() => {
|
||||
expect(getDOM().getText(body)).toEqual('Works!');
|
||||
});
|
||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}])
|
||||
.bootstrapModule(ExampleModule)
|
||||
.then((moduleRef) => {
|
||||
const doc = moduleRef.injector.get(DOCUMENT);
|
||||
expect(getDOM().getText(doc)).toEqual('Works!');
|
||||
});
|
||||
}));
|
||||
|
||||
describe('PlatformLocation', () => {
|
||||
it('is injectable', () => {
|
||||
const body = writeBody('<app></app>');
|
||||
platformDynamicServer().bootstrapModule(ExampleModule).then(appRef => {
|
||||
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
|
||||
expect(location.pathname).toBe('/');
|
||||
});
|
||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}])
|
||||
.bootstrapModule(ExampleModule)
|
||||
.then(appRef => {
|
||||
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
|
||||
expect(location.pathname).toBe('/');
|
||||
});
|
||||
});
|
||||
it('pushState causes the URL to update', () => {
|
||||
const body = writeBody('<app></app>');
|
||||
platformDynamicServer().bootstrapModule(ExampleModule).then(appRef => {
|
||||
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
|
||||
location.pushState(null, 'Test', '/foo#bar');
|
||||
expect(location.pathname).toBe('/foo');
|
||||
expect(location.hash).toBe('#bar');
|
||||
});
|
||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}])
|
||||
.bootstrapModule(ExampleModule)
|
||||
.then(appRef => {
|
||||
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
|
||||
location.pushState(null, 'Test', '/foo#bar');
|
||||
expect(location.pathname).toBe('/foo');
|
||||
expect(location.hash).toBe('#bar');
|
||||
});
|
||||
});
|
||||
it('allows subscription to the hash state', done => {
|
||||
const body = writeBody('<app></app>');
|
||||
platformDynamicServer().bootstrapModule(ExampleModule).then(appRef => {
|
||||
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
|
||||
expect(location.pathname).toBe('/');
|
||||
location.onHashChange((e: any) => {
|
||||
expect(e.type).toBe('hashchange');
|
||||
expect(e.oldUrl).toBe('/');
|
||||
expect(e.newUrl).toBe('/foo#bar');
|
||||
done();
|
||||
});
|
||||
location.pushState(null, 'Test', '/foo#bar');
|
||||
});
|
||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: '<app></app>'}}])
|
||||
.bootstrapModule(ExampleModule)
|
||||
.then(appRef => {
|
||||
const location: PlatformLocation = appRef.injector.get(PlatformLocation);
|
||||
expect(location.pathname).toBe('/');
|
||||
location.onHashChange((e: any) => {
|
||||
expect(e.type).toBe('hashchange');
|
||||
expect(e.oldUrl).toBe('/');
|
||||
expect(e.newUrl).toBe('/foo#bar');
|
||||
done();
|
||||
});
|
||||
location.pushState(null, 'Test', '/foo#bar');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Platform Server', () => {
|
||||
@Component({selector: 'app', template: '{{text}}'})
|
||||
class MyAsyncServerApp {
|
||||
text = '';
|
||||
|
||||
ngOnInit() {
|
||||
Promise.resolve(null).then(() => setTimeout(() => { this.text = 'Works!'; }, 10));
|
||||
}
|
||||
}
|
||||
|
||||
@NgModule(
|
||||
{declarations: [MyAsyncServerApp], imports: [ServerModule], bootstrap: [MyAsyncServerApp]})
|
||||
class AsyncServerModule {
|
||||
}
|
||||
|
||||
let doc: string;
|
||||
let called: boolean;
|
||||
let expectedOutput =
|
||||
'<html><head></head><body><app ng-version="0.0.0-PLACEHOLDER">Works!</app></body></html>';
|
||||
|
||||
beforeEach(() => {
|
||||
destroyPlatform();
|
||||
// PlatformConfig takes in a parsed document so that it can be cached across requests.
|
||||
doc = '<html><head></head><body><app></app></body></html>';
|
||||
called = false;
|
||||
});
|
||||
afterEach(() => {
|
||||
expect(called).toBe(true);
|
||||
// Platform should have been destroyed at the end of rendering.
|
||||
expect(getPlatform()).toBeNull();
|
||||
});
|
||||
|
||||
it('PlatformState should render to string (Long form rendering)', async(() => {
|
||||
const platform =
|
||||
platformDynamicServer([{provide: INITIAL_CONFIG, useValue: {document: doc}}]);
|
||||
|
||||
platform.bootstrapModule(AsyncServerModule)
|
||||
.then((moduleRef) => {
|
||||
const applicationRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
|
||||
return toPromise.call(first.call(
|
||||
filter.call(applicationRef.isStable, (isStable: boolean) => isStable)));
|
||||
})
|
||||
.then((b) => {
|
||||
expect(platform.injector.get(PlatformState).renderToString()).toBe(expectedOutput);
|
||||
destroyPlatform();
|
||||
called = true;
|
||||
});
|
||||
}));
|
||||
|
||||
it('renderModule should render to string (short form rendering)', async(() => {
|
||||
renderModule(AsyncServerModule, {document: doc}).then(output => {
|
||||
expect(output).toBe(expectedOutput);
|
||||
called = true;
|
||||
});
|
||||
}));
|
||||
|
||||
it('renderModuleFactory should render to string (short form rendering)',
|
||||
async(inject([PlatformRef], (defaultPlatform: PlatformRef) => {
|
||||
const compilerFactory: CompilerFactory =
|
||||
defaultPlatform.injector.get(CompilerFactory, null);
|
||||
const moduleFactory =
|
||||
compilerFactory.createCompiler().compileModuleSync(AsyncServerModule);
|
||||
renderModuleFactory(moduleFactory, {document: doc}).then(output => {
|
||||
expect(output).toBe(expectedOutput);
|
||||
called = true;
|
||||
});
|
||||
})));
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user